在 C 中检查 userID 参数时程序崩溃
Program crash in checking userID Parameter in C
您好,让我们快速解决这个问题。所以我写了这个登录程序:
void login(){
int i, j, a=0, idCheck, passCheck;
char userid[20], password[10], ch, rd;
USERDATA userArr[20];
//preparing .txt file to read
char *userData = "user.txt";
FILE *fp = fopen(userData, "r");
if(fp == NULL){
printf("\n\n\n\t\t\t\t\tError: Couldn't open file %s\n\n\n\n\n\n\n", userData);
printf("\n\n\n\t\t\t\t\tPress enter to continue\n\t\t\t\t\t");
return 1;
}
while(!feof(fp)){
USERDATA newUser;
fscanf(fp, "%[^#]#%[^#]#\n", newUser.username, newUser.password);
userArr[a] = newUser;
a++;
}
fclose(fp);
printf("\n\n\n\t\t\t\t\t============= login =============\n\n\n\n\n\n\n");
printf("\t\t\t\t\t username/email : ");scanf("%s", &userid);
printf("\t\t\t\t\t password : ");scanf("%s", &password);
for(j=0; j < a; j++){
idCheck = strcmp(userArr[j].username, userid);
passCheck = strcmp(userArr[j].password, password);
if(idCheck == 0 && passCheck == 0){
printf("\n\n\n\t\t\t\t\t Login is successfully done ");
} else {
printf("\n\n\n\t\t\t\t\t You don't have account, let's do signup\n");
printf("\n\n\n\t\t\t\t\t Press anything to continue..\n\n");
}
}
}
在这个部分
for(j=0; j < a; j++){
do{
idCheck = strcmp(userArr[j].username, userid);
passCheck = strcmp(userArr[j].password, password);
}while(passCheck != 0 && idCheck != 0);
if(idCheck == 0 && passCheck == 0){
printf("\n\n\n\t\t\t\t\t Login is successfully done");
loginMenu(userid, password);
} else if(idCheck, passCheck == 1 || idCheck, passCheck == -1){
printf("\n\n\n\t\t\t\t\t You don't have account, let's do signup\n");
printf("\n\n\n\t\t\t\t\t Press anything to continue..\n\n");
getch();
fflush(stdin);
system("cls");
} else {
printf("here");
getch();
fflush(stdin);
system("cls");
}
我想在 idCheck 和 passcheck 上做一个小的 do-while 循环,但是当我这样做时,程序最终冻结或可能崩溃。有没有更好的方法来做到这一点?我哪一部分做错了?我试过移入和移出 if 条件,但它保持不变
我不明白你为什么要使用 do-while 循环。您唯一需要做的就是记录两个变量中的比较值。 strcmp
比较整个字符串,而不仅仅是第一个字符。
修复后损坏的部分应该如下所示:
for(j=0; j < a; j++){
idCheck = strcmp(userArr[j].username, userid);
passCheck = strcmp(userArr[j].password, password);
// ...
您好,让我们快速解决这个问题。所以我写了这个登录程序:
void login(){
int i, j, a=0, idCheck, passCheck;
char userid[20], password[10], ch, rd;
USERDATA userArr[20];
//preparing .txt file to read
char *userData = "user.txt";
FILE *fp = fopen(userData, "r");
if(fp == NULL){
printf("\n\n\n\t\t\t\t\tError: Couldn't open file %s\n\n\n\n\n\n\n", userData);
printf("\n\n\n\t\t\t\t\tPress enter to continue\n\t\t\t\t\t");
return 1;
}
while(!feof(fp)){
USERDATA newUser;
fscanf(fp, "%[^#]#%[^#]#\n", newUser.username, newUser.password);
userArr[a] = newUser;
a++;
}
fclose(fp);
printf("\n\n\n\t\t\t\t\t============= login =============\n\n\n\n\n\n\n");
printf("\t\t\t\t\t username/email : ");scanf("%s", &userid);
printf("\t\t\t\t\t password : ");scanf("%s", &password);
for(j=0; j < a; j++){
idCheck = strcmp(userArr[j].username, userid);
passCheck = strcmp(userArr[j].password, password);
if(idCheck == 0 && passCheck == 0){
printf("\n\n\n\t\t\t\t\t Login is successfully done ");
} else {
printf("\n\n\n\t\t\t\t\t You don't have account, let's do signup\n");
printf("\n\n\n\t\t\t\t\t Press anything to continue..\n\n");
}
}
}
在这个部分
for(j=0; j < a; j++){
do{
idCheck = strcmp(userArr[j].username, userid);
passCheck = strcmp(userArr[j].password, password);
}while(passCheck != 0 && idCheck != 0);
if(idCheck == 0 && passCheck == 0){
printf("\n\n\n\t\t\t\t\t Login is successfully done");
loginMenu(userid, password);
} else if(idCheck, passCheck == 1 || idCheck, passCheck == -1){
printf("\n\n\n\t\t\t\t\t You don't have account, let's do signup\n");
printf("\n\n\n\t\t\t\t\t Press anything to continue..\n\n");
getch();
fflush(stdin);
system("cls");
} else {
printf("here");
getch();
fflush(stdin);
system("cls");
}
我想在 idCheck 和 passcheck 上做一个小的 do-while 循环,但是当我这样做时,程序最终冻结或可能崩溃。有没有更好的方法来做到这一点?我哪一部分做错了?我试过移入和移出 if 条件,但它保持不变
我不明白你为什么要使用 do-while 循环。您唯一需要做的就是记录两个变量中的比较值。 strcmp
比较整个字符串,而不仅仅是第一个字符。
修复后损坏的部分应该如下所示:
for(j=0; j < a; j++){
idCheck = strcmp(userArr[j].username, userid);
passCheck = strcmp(userArr[j].password, password);
// ...