我正在用C语言做一个注册码,基本上可以输入多个账户。 现在,我想要的是通过登录函数访问该特定帐户,其中(Pass == LogIn Pass)。
虽然我的代码已经可以访问该特定帐户(请参阅 Inquiry() 函数),但我的问题是,当我尝试使用该特定帐户的密码确认 LogIn_Pass 时,它总是返回“找不到帐户”,即使用户输入是正确的。
我已经检查过 Inquiry() 是否有效,它是基于 LogIn() 中输入的帐号。
我的问题是LogIn_Pass 无法与注册时输入的密码(accounts[i][4])进行比较。
请参阅登录功能。 “欢迎”声明永远不会打印。我怎样才能成功地比较它们?
我是 C 语言新手,我被难住了。请帮忙。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char accounts[100][4][100];
int num_accounts = 0, confirm_pass, login_pass;
int acc_num;
int login_pass;
// Function declarations
void Registration();
void Inquiry();
void LogIn();
//Main Function
int main() {
int n;
Registration();
LogIn();
while (1) {
printf("\n1. Inquiry\n2. Exit\n");
scanf("%d", &n);
switch (n) {
case 1:
Inquiry();
break;
case 2:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
//Registration Function
void Registration() {
printf("Enter the number of accounts to register: ");
scanf("%d", &num_accounts);
for (int i = 0; i < num_accounts; i++) {
getchar();
printf("\nRegistration for Account %d\n", i + 1);
printf("____________________________\n");
printf("1. Account name: ");
fgets(accounts[i][0], sizeof(accounts[i][0]), stdin);
printf("2. Account number: ");
fgets(accounts[i][1], sizeof(accounts[i][1]), stdin);
printf("3. Address: ");
fgets(accounts[i][2], sizeof(accounts[i][2]), stdin);
printf("4. Mobile number: ");
fgets(accounts[i][3], sizeof(accounts[i][3]), stdin);
// Prompt for password creation
do {
printf("\n\nCreate Password: ");
fgets(accounts[i][4], sizeof(accounts[i][4]), stdin);
printf("Confirm Password: ");
fgets(accounts[i][5], sizeof(accounts[i][5]), stdin);
if (strcmp(accounts[i][4], accounts[i][5]) == 0) {
break;
} else {
printf("Password does not match!\n");
}
} while (1);
}
}
//LogIn Function
void LogIn() {
printf("\nLog In: \n");
printf("____________________________\n");
printf("Account number: ");
scanf("%d", &acc_num);
printf("Password: ");
scanf("%d", &login_pass);
// Find the account with the matching account number
int found = 0;
for (int i = 0; i < num_accounts; i++) {
if (acc_num == atoi(accounts[i][1]) && login_pass == atoi(accounts[i][4])) {
printf("\nWelcome!\n");
found = 1;
break;
}
}
if (!found) {
printf("\nAccount not found!\n");
}
}
//Inquiry Function
void Inquiry() {
for (int i = 0; i < num_accounts; i++) {
if (acc_num == atoi(accounts[i][1])) {
printf("Account name: %s", accounts[i][0]);
printf("Account number: %s", accounts[i][1]);
printf("Address: %s", accounts[i][2]);
printf("Mobile number: %s", accounts[i][3]);
break;
}
}
}
OP:“我怎样才能成功地比较它们?”
答案:不覆盖它们。
char accounts[100][4][100];
显示您要记录 4 个字段的 100 条记录,每个字段长 100 个字符。
然后,创建帐户时:
和...fgets(accounts[i][4], sizeof(accounts[i][4]), stdin);
fgets(accounts[i][5], sizeof(accounts[i][5]), stdin);
将信息记录到想象的第 5 行和第 6 行(即:超出预期的 4 行数据)。
最小修复:使用
enum
标记来命名每个“记录”的“行”,而不是“幻数”
enum { eName, eAcct, eAddr, eMobl, ePswd, nFlds };
注意: 将这些编译器标记的定义放置在源代码的 top 处(在
#include
行之后)。
然后,类似:
printf("1. Account name: ");
fgets(accounts[i][ eName ], sizeof(accounts[i][ eName ]), stdin);
printf("2. Account number: ");
fgets(accounts[i][ eAcct ], sizeof(accounts[i][ eAcct ]), stdin);
printf("3. Address: ");
fgets(accounts[i][ eAddr ], sizeof(accounts[i][ eAddr ]), stdin);
printf("4. Mobile number: ");
fgets(accounts[i][ eMobl ], sizeof(accounts[i][ eMobl ]), stdin);
// Prompt for password creation
do {
printf("\n\nCreate Password: ");
fgets(accounts[i][ ePswd ], sizeof(accounts[i][ ePswd ]), stdin);
printf("Confirm Password: ");
char tmp[ sizeof accounts[i][ ePswd ] ];
fgets( tmp, tmp, stdin);
if (strcmp( accounts[i][ ePswd ], tmp ) == 0)
break;
printf("Password does not match!\n");
} while (1);
然后修复分配线:
char accounts[100][ nFlds ][100];
太多“新手乐观主义”,没有仔细关注每一个重要细节。
此处显示的代码尚未编译或测试。仅用于教学目的。