I am doing a Registration code in C, which basically can input multiple accounts.
Now, what I want is to access that specific account through a Log-In function where (Pass == LogIn Pass).
Although my code can already access that specific account (see Inquiry() function), my problem is that when I try to confirm the LogIn_Pass with the password of that specific account, it always returns “Account not Found” even though the user input is correct.
Please see the LogIn Function. The ‘welcome’ statement never prints. How can I successfully compare them?
Im new in C, and Im stumped. Please help.
#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. Inquiryn2. Exitn");
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 %dn", 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("nnCreate 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;
}
}
}