代码没报错,但是输入一样的密码的时候总是显示密码错误,请各位专家指导,是没有保存到文件里面的原因吗
建议把代码发出来,图片看得不是很懂。
#include <iostream>
#include<Windows.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
typedef struct The_users
{
char id[100];
char password[20];
long phone[11];
char mailbox[100];
}users;
void Create_File()
{
FILE* fp;
errno_t err;
if ((err = fopen_s(&fp, "users.txt", "rb")) == NULL)
{
if ((err = fopen_s(&fp, "users.txt", "wb+")) == NULL)
{
printf("无法建立文件!\n");
exit(0);
}
}
}
void registers()
{
users a, b;
FILE* fp;
errno_t err;
int count = 0;
printf("欢迎来到注册界面!\n");
Sleep(1000);
err = fopen_s(&fp, "users.txt", "r");
if(fp!=NULL)fread(&b, sizeof(struct The_users), 1, fp);
printf("请输入用户名,用户名不可重复(输入用户名后请按回车+任意数字+回车进行下一步)");
scanf_s("%s", &a.id, (int)sizeof(a.id));
if (strcmp(a.id, b.id))
{
if(fp!=NULL) if (!feof(fp))
{
fread(&b, sizeof(struct The_users), 1, fp);
}
}
else
{
printf("用户名输入有误,与已有用户重复,请重新输入\n");
}
scanf_s("%s", &a.id, (int)sizeof(a.id));
printf("请输入密码,密码不得只有数字且小于六位(输入密码后请按回车+任意数字+回车进行下一步)\n");
scanf_s(" %s", &a.password, (int)sizeof(a.password));
int i;
int j = 0;
for (i = 0; i < strlen(a.password); i++)
{
if (strlen(a.password) < 6 || (a.password[i] >= '0' && a.password[i] <= '9'))
printf("密码输入有误,密码最少6位数并且不能只有数字,请重新输入!");
scanf_s("%s", &a.password, (int)sizeof(a.password));
}
printf("请输入电话\n");
scanf_s("%d", a.phone);
printf("请输入邮箱:\n");
scanf_s("%s", &a.mailbox, (int)sizeof(a.mailbox));
printf("恭喜!您的账号注册成功,请登录!\n");
Sleep(500);
if (fp != NULL)fclose(fp);
return;
}
void login()
{
users a, b;
errno_t err;
FILE* fp;
printf("欢迎来到登录界面\n");
Sleep(1000);
err = fopen_s(&fp, "users.txt", "r");
if (fp != NULL)fread(&b, sizeof(struct The_users), 1, fp);
printf("请输入用户名");
scanf_s("%s", &b.id, (int)sizeof(b.id));
{
if (fp != NULL)if (!feof(fp))
{
fread(&b, sizeof(struct The_users), 1, fp);
}
else
{
printf("没有名为&b.id的用户\n");
Sleep(500);
if (fp != NULL)fclose(fp);
return;
}
}
printf("请输入密码\n");
scanf_s("%s", &b.password, (int)sizeof(b.password));
do {
if (strcmp(a.password, b.password) == 0)
{
if (fp != NULL)fclose(fp);
printf("登录成功,欢迎使用!\n");
Sleep(500);
return;
}
else
{
printf("密码错误!请重新输入密码\n");
scanf_s("%s", &b.password, (int)sizeof(b.password));
}
} while (strcmp(a.password, b.password) == 0);
}
int main()
{
int n;
while (1)
{
printf("***********菜单*************\n");
printf("** 1:登录 **\n");
printf("** 2:注册 **\n");
printf("** 3:退出 **\n");
printf("***************************\n");
printf("请输入1-3编号:");
scanf_s("%d", &n);
if (n == 1)
{
login();
}
else if (n == 2)
{
registers();
}
else if (n == 3)
{
break;
}
}
system("pause");
return 0;
}
scanf 输入字符串不用&的吧
修改如下,供参考:
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct The_users
{
char id[100];
char password[20];
char phone[12];
char mailbox[100];
struct The_users* next;
}users;
void add_users(char* s = NULL)
{
FILE* fp;
errno_t err;
int count = 0, i = 0, flg = 0, flag = 0;
char name_id[100], password[20], phone[12], mailbox[100];
if ((err = fopen_s(&fp, "users.txt", "a")) != 0)
printf("文件打开失败!\n");
else {
if (s == NULL){
printf("新建用户名:");
scanf_s("%s", name_id, (int)sizeof(name_id));
}
else
strcpy(name_id, s);
do {
printf("请输入密码(六位[数字+字符]):");
scanf_s("%s", password, (int)sizeof(password));
count = strlen(password);
for (i = 0, flg = 0, flag = 0; i < count; i++)
if (isdigit(password[i]))
flg++;
else
flag++;
} while (count != 6 || flg == 0 || flag == 0);
printf("请输入电话:");
scanf_s("%s", phone, (int)sizeof(phone));
printf("请输入邮箱:");
scanf_s("%s", mailbox, (int)sizeof(mailbox));
fprintf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox);
fclose(fp);
printf("恭喜,您的账号注册成功!\n");
}
}
void registers()
{
char name_id[100], password[20], phone[12], mailbox[100];
users* head, * tail, * p;
FILE* fp;
errno_t err;
printf("欢迎来到注册界面!\n");
if ((err = fopen_s(&fp, "users.txt", "r")) != 0){
add_users();
}
else {
head = NULL, tail = NULL, p = NULL;
while (1)
{
if (fscanf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox) != 4) break;
p = (users*)malloc(sizeof(struct The_users));
p->next = NULL;
strcpy(p->id, name_id); strcpy(p->password, password);
strcpy(p->phone, phone); strcpy(p->mailbox, mailbox);
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
fclose(fp);
do {
printf("新建用户名:");
scanf("%s", name_id);
tail = head;
while (tail != NULL && strcmp(tail->id, name_id) != 0)
{
tail = tail->next;
}
if (tail == NULL)
add_users(name_id);
else
printf("该用户名已经被注册,请重新输入!\n");
} while (tail != NULL);
}
return;
}
int login()
{
char name_id[100], password[20], phone[12], mailbox[100];
users* head, * tail, * p;
errno_t err;
FILE* fp;
int i = 0, flg = 0;
if ((err = fopen_s(&fp, "users.txt", "r")) != 0){
printf("无注册用户,请先注册用户。\n");
}
else {
printf("欢迎来到登录界面\n");
head = NULL, tail = NULL, p = NULL;
while (1)
{
if (fscanf(fp, "%s %s %s %s\n", name_id, password, phone, mailbox) != 4) break;
p = (users*)malloc(sizeof(struct The_users));
p->next = NULL;
strcpy(p->id, name_id); strcpy(p->password, password);
strcpy(p->phone, phone); strcpy(p->mailbox, mailbox);
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
fclose(fp);
printf("请输入用户名:");
scanf_s("%s", name_id, (int)sizeof(name_id));
tail = head;
while (tail != NULL && strcmp(tail->id, name_id) != 0)
{
tail = tail->next;
}
if (tail != NULL)
{
do {
printf("密 码:");
scanf_s("%s", password, (int)sizeof(password));
if (strcmp(tail->password, password) == 0)
{
printf("登录成功!\n");
return 1;
}
else {
i++;
if (i != 3)
printf("输入密码错误,还有%d次机会,请重新输入!\n", 3 - i);
else {
printf("输入密码错误,3次机会已用完,退出登录!\n");
break;
}
}
} while (strcmp(tail->password, password) != 0);
}
else
printf("该用户名不存在!\n");
}
return 0;
}
int main()
{
int n;
while (1)
{
printf("***********菜单*************\n");
printf("** 1:登录 **\n");
printf("** 2:注册 **\n");
printf("** 3:退出 **\n");
printf("***************************\n");
printf("请输入1-3编号:");
scanf_s("%d", &n);
if (n == 2)
registers();
else if (n == 3)
break;
else if (n == 1)
login();
}
return 0;
}