检查c中userID参数时程序崩溃



你好,让我们快速解决这个问题。这里是我写的登录程序

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("nnntttttError: Couldn't open file %snnnnnnn", userData);
printf("nnntttttPress enter to continuenttttt");
return 1;
}
while(!feof(fp)){
USERDATA newUser;
fscanf(fp, "%[^#]#%[^#]#n", newUser.username, newUser.password);
userArr[a] = newUser;                                                   
a++;
}
fclose(fp);
printf("nnnttttt=============    login    =============nnnnnnn");
printf("ttttt username/email   :    ");scanf("%s", &userid);
printf("ttttt 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("nnnttttt Login is successfully done    ");
} else {
printf("nnnttttt You don't have account, let's do signupn");
printf("nnnttttt Press anything to continue..nn");

}
}
}

本节

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("nnnttttt  Login is successfully done");
loginMenu(userid, password);
} else if(idCheck, passCheck == 1 || idCheck, passCheck == -1){
printf("nnnttttt You don't have account, let's do signupn");
printf("nnnttttt Press anything to continue..nn");
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);
// ...

相关内容

  • 没有找到相关文章

最新更新