1.这是我用while写的代码。
为什么我的在三次错误时无法打印出“已锁定”?
为什么我的在输入正确时无法打印出“对”?
为什么我的在输入错误时无法打印出“错”?
2.这是某up主用for写的代码。这两种方法都行吗?
供参考:
#include <string.h>
#include <stdio.h>
struct yhxx
{
char name[10];
char password[12];
}student[5] = { {"abcdefg","12345678"},{"aabbccdd","987654321"} };
int main()
{
int i = 0, j;
char name[10], password[12];
while (1)
{
printf("输入学生名字:");
scanf("%s", name);
printf("请输入密码:");
scanf("%s", password);
for (j = 0; j < 5; j++) {
if (strcmp(student[j].name, name) == 0 &&
strcmp(student[j].password, password) == 0)
{
printf("已成功登录!\n");
break;
}
}
if (j >= 5){
i++;
if (i < 3)
printf("用户名或密码错误!还有%d次机会重新输入.\n", 3 - i);
else {
printf("退出登录!\n");
break;
}
}
else
break;
}
return 0;
}
scanf前加两句:
fflush(stdout);
rewind(stdin);