最近我一直在开发一个项目,该项目为我提供了一个文件,其中包含一个包含 16 个数字的整数,后跟一个包含 4 个数字的整数(卡号和密码)。我的目标是创建一个用户密码系统,在 3 次登录尝试失败后就会关闭。现在,这不是问题,我的问题是,无论我如何编写代码,我的输入(无论是卡号还是密码)都不会与文件值匹配。这是我的代码
typedef struct{
int number[16];
int pass[4];
} TypeCard;
int main(void)
{
int i, c = 0, n, p, cards;
TypeCard card;
FILE *f = fopen("cards.txt", "r");
for(i = 0; fscanf(f, "%d %d", card.number, card.pass)!= EOF; i++)
{
cards++; /*this is merely to fill this loop, i don't know if ill need it*/
}
fclose(f);
while(c != 3) /*c is a counter */
{
int al = 2 - c; /*al = attempts left*/
printf("Insert card number: ");
scanf("%d", &n);
if(n == card.number[i])
{
printf("Insert password: ");
scanf("%d", &p);
if(p == card.pass[i])
{
printf("Access granted\n");
return 0;
}
else
{
printf("Wrong password\n");
printf("Attempts left: %d\n", al);
c++;
}
}
else
{
printf("Invalid card\n");
printf("Attempts left: %d\n", al);
printf("Try again.\n\n");
c++;
}
}
printf("Access blocked. Please contact the system administrator.\n");
return 0;
}
有人可以帮我吗?
最佳答案
您正在尝试将一个 16 位数字读入一个最多 32 位的 int 中,该整数只能处理 10 位数字。要处理 16 位数字,您需要使用 64 位的“long long”,可以处理 20 位数字。
关于c - 为什么插入的用户值与文件值不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41337483/