In: Computer Science
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<conio.h>
struct Bank_Account_Holder
{
int account_no;
char name[80];
int balance;
};
int n;
void accept(struct Bank_Account_Holder[], int);
void display(struct Bank_Account_Holder[], int);
void save(struct Bank_Account_Holder[], int);
void load(struct Bank_Account_Holder[], int);
int search(struct Bank_Account_Holder[], int, int);
void deposit(struct Bank_Account_Holder[], int, int, int);
void withdraw(struct Bank_Account_Holder[], int, int, int);
int lowBalenquiry(int,int);
void main(void)
{
clrscr();
struct Bank_Account_Holder data[20];
int choice, account_no, amount, index;
printf("NHU Banking System\n\n");
printf("Enter the count of records: ");
scanf("%d", &n);
accept(data, n);
do
{
printf("\nNHU Banking System Menu :\n");
printf("Press 1 to display all records.\n");
printf("Press 2 to search a record.\n");
printf("Press 3 to deposit amount.\n");
printf("Press 4 to withdraw amount.\n");
printf("Press 5 to save all records to
file.\n");
printf("Press 6 to load Records from file.\n");
printf("Press 0 to exit\n");
printf("\nEnter choice(0-4) : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
display(data, n);
break;
case 2:
printf("Enter account number to
search : ");
scanf("%d", &account_no);
index = search(data, n,
account_no);
if (index == - 1)
{
printf("Record not found :
");
}
else
{
printf("A/c Number: %d\nName:
%s\nBalance: %d\n",
data[index].account_no, data[index].name,
data[index].balance);
}
break;
case 3:
printf("Enter account number :
");
scanf("%d", &account_no);
printf("Enter amount to deposit :
");
scanf("%d", &amount);
deposit(data, n, account_no,
amount);
break;
case 4:
printf("Enter account number :
");
scanf("%d", &account_no);
printf("Enter amount to withdraw :
");
scanf("%d", &amount);
withdraw(data, n, account_no,
amount);
break;
case 5:
save(data, n);
break;
case 6:
load(data, n);
break;
default:
printf("\nWrong choice");
case 0:
exit(1);
}
}
while (choice != 0);
getche();
}
void accept(struct Bank_Account_Holder array[80], int s)
{
int i;
for (i = 0; i < s; i++)
{
printf("\nEnter data for Record #%d", i + 1);
printf("\n Enter account_no : ");
scanf("%d", &array[i].account_no);
fflush(stdin);
printf("Enter name of account Holder : ");
gets(array[i].name);
array[i].balance = 0;
}
}
void load(struct Bank_Account_Holder array[80], int s)
{
int i;
char fname[20];
printf("\nEnter File with extention for loading::");
scanf("%s",&fname);
FILE * fp;
char dataToBeRead[50];
fp = fopen(fname, "r");
if ( fp == NULL )
{
printf( "\n File Cannot Find" ) ;
}
else
{
printf("\n The file is now opened.\n") ;
// Read the dataToBeRead from the file
// using fgets() method
while( fgets ( dataToBeRead, 50, fp ) != NULL )
{
char *ptr = strtok(dataToBeRead, ",");
array[n].account_no=atoi(ptr);
ptr = strtok(NULL, ",");
//printf("'%s'\n", ptr);
strcpy(array[n].name,ptr);
ptr = strtok(NULL, ",");
//printf("'%s'\n", ptr);
array[n].balance=atoi(ptr);
ptr = strtok(NULL, ",");
n=n+1;
}
// Closing the file using fclose()
fclose(fp) ;
printf("Data successfully read from file\n");
printf("The file is now closed.") ;
}
}
void display(struct Bank_Account_Holder array[80], int s)
{
int i;
printf("\n\nA/c No\tName\tBalance\n");
for (i = 0; i < s; i++)
{
printf("%d\t%s\t%d\n", array[i].account_no, array[i].name,
array[i].balance);
}
}
// save records to file
void save(struct Bank_Account_Holder array[80], int s)
{
int i;
FILE *fp ;
fp = fopen("save.txt", "w");
for (i = 0; i < s; i++)
{
//each line represent single record and field seperated by
comma
fprintf(fp, "%d,%s,%d\n", array[i].account_no, array[i].name,
array[i].balance);
}
fclose(fp);
printf("\nSaved Sucessfully to file");
}
int search(struct Bank_Account_Holder array[80], int s, int
number)
{
int i;
for (i = 0; i < s; i++)
{
if (array[i].account_no == number)
{
return i;
}
}
return - 1;
}
void deposit(struct Bank_Account_Holder array[], int s, int
number, int amt)
{
int i = search(array, s, number);
if (i == - 1)
{
printf("Record not found");
}
else
{
array[i].balance += amt;
}
}
void withdraw(struct Bank_Account_Holder array[], int s, int
number, int amt)
{
int i = search(array, s, number);
if (i == - 1)
{
printf("Record not found\n");
}
else if (lowBalenquiry(array[i].balance,amt))
{
printf("Insufficient balance\n");
}
else
{
array[i].balance -= amt;
}
}
int lowBalenquiry(int bal,int amt){
if(bal < amt)
return 1;
return 0;
}
Create a report of this program
and Explain each section of code ( I need this for semester project
report file )
Analysis: This program needs to be corrected first because this
program code contains multiple errors. And without rectifying the
errors this program cannot be executed successfully.
Test Result:
main.c: In function ‘main’:
main.c:100:1: warning: implicit declaration of function ‘getche’
[-Wimplicit-function-declaration]
getche();
^~~~~~
main.c: In function ‘accept’:
main.c:114:1: warning: ‘gets’ is deprecated
[-Wdeprecated-declarations]
gets(array[i].name);
^~~~
In file included from main.c:9:0:
/usr/include/stdio.h:638:14: note: declared here
extern char *gets (char *__s) __wur __attribute_deprecated__;
^~~~
main.c: In function ‘load’:
main.c:125:9: warning: format ‘%s’ expects argument of type ‘char
*’, but argument 2 has type ‘char (*)[20]’ [-Wformat=]
scanf("%s",&fname);
^~~~
/tmp/cc1vv8XU.o: In function `accept':
main.c:(.text+0x40f): warning: the `gets' function is dangerous and
should not be used.
/tmp/cc1vv8XU.o: In function `main':
main.c:(.text+0xc): undefined reference to `clrscr'
main.c:(.text+0x347): undefined reference to `getche'
collect2: error: ld returned 1 exit status
Conclusion: This program code can be used only after rectifying the
proposed corrections.