So we are trying to make a simple banking program in C language. There is a text file called AccountInfo.txt that contains information like account holders name, account number, account type, nid, phone number etc. One of the features of the program is a single user can have multiple bank accounts. So I was trying to implement that feature via an user defined function called SearchAndFind that will search the text files and print all the account numbers that matches with the user provided username. Here's the code: (Please note that the code is still incomplete and inefficient as well. Kindly ignore that)
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define USER_PASS "./username.txt"
#define ACCOUNT_DATA "./AccountInfo.txt"
typedef struct
{
char Name[30];
char AccountType[20];
long long AccountNo;
long long Balance;
long long Phone;
long long NID;
char Username[30];
} AccountInfo;
int loginverify(char *userid, char *pass);
void createaccount();
void mainmenu(char *usernm);
int ReadAccountInfo();
void ViewAccounts(char *usrnm, int sumaccounts);
void SearchAndPrint(char *username);
int main()
{
int flag, signinoption;
char user_name[20], password[30];
printf("Welcome to My Bank\n");
while (1)
{
printf("1. Login\n");
printf("2. Create a new account\n");
printf("3. Exit\n");
printf("Choose your option: ");
scanf("%d", &signinoption);
printf("\n");
switch (signinoption)
{
case 1:
while (1)
{
printf("Enter Username: ");
scanf("%19s", user_name);
printf("Enter Password: ");
scanf("%29s", password);
flag = loginverify(user_name, password);
if (flag == 1)
{
printf("\nLogin successful. \n");
printf("\n");
mainmenu(user_name);
break;
}
else
{
printf("Login Failed. Try again. \n");
printf("\n");
}
}
break;
case 2:
createaccount();
break;
case 3:
return 0;
break;
}
if (signinoption == 1)
break;
}
return 0;
}
void mainmenu(char *usernm)
{
int menuchoice = 100;
char usrnm[30];
strcpy(usrnm, usernm);
while (menuchoice != 12)
{
printf("1. View All Accounts\n");
printf("2. Check Balance\n");
printf("3. Cash Deposit\n");
printf("4. Cash Withdraw\n");
printf("5. Fund Transfer\n");
printf("6. Benificiary Management\n");
printf("7. Mini Statement\n");
printf("8. Account Settings\n");
printf("9. Close Account\n");
printf("10. ATM/Branch Locations\n");
printf("11. Customer Support\n");
printf("12. Exit\n");
printf("\n");
printf("Choose your option: ");
scanf("%d", &menuchoice);
printf("\n");
if (menuchoice == 1)
SearchAndPrint(usrnm);
}
}
int loginverify(char *userid, char *pass)
{
FILE *fp;
char user[20], password[30];
int found = 0;
fp = fopen(USER_PASS, "r");
if (fp == NULL)
{
printf("Error: Could not open file\n");
return 0;
}
while (fscanf(fp, "Username: %s\nPassword: %s\n", user, password) == 2)
{
if (strcmp(userid, user) == 0 && strcmp(pass, password) == 0)
{
found = 1;
break;
}
}
fclose(fp);
return found;
}
void createaccount()
{
char username[20], password[20];
printf("Enter your login credentials: \n");
// a function can be added to check uniquenness
printf("Enter Username: ");
scanf("%s", username);
printf("Enter Password: ");
scanf("%s", password);
printf("\n");
printf("Enter your Account details: \n");
printf("Enter your Name: ");
printf("Enter your Account Type (Savings/Current/Fixed Deposit): ");
// here unique account number is requires. or it can be generated automatically
printf("Enter your Account No: ");
printf("Enter your Balance: ");
printf("Enter your Phone No: ");
printf("Enter your NID No: ");
printf("\n");
printf("Account Created Successfully\n");
printf("\n");
}
void SearchAndPrint(char *username)
{
printf("\n%s\n", username);
FILE *fp;
AccountInfo acc;
int found = 0;
fp = fopen(ACCOUNT_DATA, "r");
if (fp == NULL)
{
printf("Error: Could not open file\n");
return;
}
while (fscanf(fp, "Name: %s\nAccount Type: %s\nAccount No: %lld\nBalance: %lld\nPhone: %lld\nNID No: %lld\nUsername: %s\n", acc.Name, acc.AccountType, &acc.AccountNo, &acc.Balance, &acc.Phone, &acc.NID, acc.Username) == 7)
{
printf("Name: %s\nAccount Type: %s\nAccount No: %lld\nBalance: %lld\nPhone: %lld\nNID No: %lld\nUsername: %s\n", acc.Name, acc.AccountType, acc.AccountNo, acc.Balance, acc.Phone, acc.NID, acc.Username);
if (strcmp(username, acc.Username) == 0)
{
found = 1;
printf("Account No: %lld\nAccount Type: %s\n", acc.AccountNo, acc.AccountType);
}
}
fclose(fp);
if (!found)
{
printf("No matching accounts found\n");
}
}
Now from main menu when I input 1, it goes to the SearchAndPrint function, then executes the line: printf("\n%s\n", username); but nothing afterwards. I have tried several methods but nothing works. My assumption there must be some problem reading the contents of file.
Here is what AccountInfo.txt file contains: https://pastebin.com/y2qypf3s
For login credentials, you can use any of the following: https://pastebin.com/iF1BTuZB
I tried different variants of while loop to take input from file. I was expecting to print out the Account Informations of all the entries that matches with the provided username.
In trying out your program, I also could not get any account information to list due to the fact of the account name actually being comprised of string with an embedded blank. Having worked with the "scanf" function and strings with embedded blanks in previous code, I did try out some refactored code utilizing the formatting literal "%[^\n]" as noted in the good comments above. However, in order to make that formatting literal work entailed a tweak to the account information file as well.
First off, following is a refactored version of your program with the refinement to the "fscanf" function.
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define USER_PASS "./username.txt"
#define ACCOUNT_DATA "./AccountInfo.txt"
typedef struct
{
char Name[30];
char AccountType[20];
long long AccountNo;
long long Balance;
long long Phone;
long long NID;
char Username[30];
} AccountInfo;
int loginverify(char *userid, char *pass);
void createaccount();
void mainmenu(char *usernm);
int ReadAccountInfo();
void ViewAccounts(char *usrnm, int sumaccounts);
void SearchAndPrint(char *username);
int main()
{
int flag, signinoption;
char user_name[20], password[30];
printf("Welcome to My Bank\n");
while (1)
{
printf("1. Login\n");
printf("2. Create a new account\n");
printf("3. Exit\n");
printf("Choose your option: ");
scanf("%d", &signinoption);
printf("\n");
switch (signinoption)
{
case 1:
while (1)
{
printf("Enter Username: ");
scanf("%19s", user_name);
printf("Enter Password: ");
scanf("%29s", password);
flag = loginverify(user_name, password);
if (flag == 1)
{
printf("\nLogin successful. \n");
printf("\n");
mainmenu(user_name);
break;
}
else
{
printf("Login Failed. Try again. \n");
printf("\n");
}
}
break;
case 2:
createaccount();
break;
case 3:
return 0;
break;
}
if (signinoption == 1)
break;
}
return 0;
}
void mainmenu(char *usernm)
{
int menuchoice = 100;
char usrnm[30];
strcpy(usrnm, usernm);
while (menuchoice != 12)
{
printf("1. View All Accounts\n");
printf("2. Check Balance\n");
printf("3. Cash Deposit\n");
printf("4. Cash Withdraw\n");
printf("5. Fund Transfer\n");
printf("6. Benificiary Management\n");
printf("7. Mini Statement\n");
printf("8. Account Settings\n");
printf("9. Close Account\n");
printf("10. ATM/Branch Locations\n");
printf("11. Customer Support\n");
printf("12. Exit\n");
printf("\n");
printf("Choose your option: ");
scanf("%d", &menuchoice);
printf("\n");
if (menuchoice == 1)
SearchAndPrint(usrnm);
}
}
int loginverify(char *userid, char *pass)
{
FILE *fp;
char user[20], password[30];
int found = 0;
fp = fopen(USER_PASS, "r");
if (fp == NULL)
{
printf("Error: Could not open file\n");
return 0;
}
while (fscanf(fp, "Username: %s\nPassword: %s\n", user, password) == 2)
{
if (strcmp(userid, user) == 0 && strcmp(pass, password) == 0)
{
found = 1;
break;
}
}
fclose(fp);
return found;
}
void createaccount()
{
char username[20], password[20];
printf("Enter your login credentials: \n");
// a function can be added to check uniquenness
printf("Enter Username: ");
scanf("%s", username);
printf("Enter Password: ");
scanf("%s", password);
printf("\n");
printf("Enter your Account details: \n");
printf("Enter your Name: ");
printf("Enter your Account Type (Savings/Current/Fixed Deposit): ");
// here unique account number is requires. or it can be generated automatically
printf("Enter your Account No: ");
printf("Enter your Balance: ");
printf("Enter your Phone No: ");
printf("Enter your NID No: ");
printf("\n");
printf("Account Created Successfully\n");
printf("\n");
}
void SearchAndPrint(char *username)
{
printf("User Name: %s\n", username);
FILE *fp;
AccountInfo acc;
int found = 0;
fp = fopen(ACCOUNT_DATA, "r");
if (fp == NULL)
{
printf("Error: Could not open file\n");
return;
}
while (fscanf(fp, "Name: %[^\n] \nAccount Type: %s\nAccount No: %lld\nBalance: %lld\nPhone: %lld\nNID No: %lld\nUsername: %s\n", acc.Name, acc.AccountType, &acc.AccountNo, &acc.Balance, &acc.Phone, &acc.NID, acc.Username) == 7)
{
if (strcmp(username, acc.Username) == 0)
{
printf("Name: %s\nAccount Type: %s\nAccount No: %lld\nBalance: %lld\nPhone: %lld\nNID No: %lld\n\n", acc.Name, acc.AccountType, acc.AccountNo, acc.Balance, acc.Phone, acc.NID);
found = 1;
}
}
fclose(fp);
if (!found)
{
printf("No matching accounts found\n");
}
}
Some key bits to note.
With those tweaks, following was the terminal output testing out the program for an account.
@Vera:~/C_Programs/Console/Banking/bin/Release$ ./Banking
Welcome to My Bank
1. Login
2. Create a new account
3. Exit
Choose your option: 1
Enter Username: ElizabethSmith
Enter Password: G7FkPvHd3m
Login successful.
1. View All Accounts
2. Check Balance
3. Cash Deposit
4. Cash Withdraw
5. Fund Transfer
6. Benificiary Management
7. Mini Statement
8. Account Settings
9. Close Account
10. ATM/Branch Locations
11. Customer Support
12. Exit
Choose your option: 1
User Name: ElizabethSmith
Name: Elizabeth Smith
Account Type: Savings
Account No: 6549873214
Balance: 93750
Phone: 8801912345678
NID No: 124356789
1. View All Accounts
2. Check Balance
3. Cash Deposit
4. Cash Withdraw
5. Fund Transfer
6. Benificiary Management
7. Mini Statement
8. Account Settings
9. Close Account
10. ATM/Branch Locations
11. Customer Support
12. Exit
Choose your option: 12
Give those tweaks a try and see if it meets the spirit of your project.