最近我一直致力于一个项目,该项目给我一个包含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;
}
有人可以帮我吗?
答案 0 :(得分:2)
您正在尝试将一个16位数字读入一个最多32位且只能处理10位数的int。要处理16位数字,您需要使用64位的“long long”,可以处理20位数字。