I can't run my program using this code. Please help me to edit it with the same function. Thanks in advance.
#include<stdio.h>
#include <stdlib.h>
main()
{
FILE * fp;
int bmenu,amenu,an;
float balance=0,deposit=0,withdraw=0;
char c,quit,name[50],address[50];
clrscr();
printf("**********BANK MENU**********\n");
printf(" ---- ---- \n");
printf("What do you want to do?\n");
printf("[1] Log-in Account\n[2] Create Account\n[3] Quit\n\nEnter the number of choice: \n");
scanf("%d",&bmenu);
printf("_____________________________\n");
if (bmenu==1)
{
printf("Account Number: ");
scanf("%d",&an);
clrscr();
printf("************Account Menu************\n");
printf(" ------- ---- \n");
printf("[1] Deposit\n[2] Withdraw\n[3] View Account Details\n[4] Quit\n\nEnter the number of choice: \n");
scanf("%d",&amenu);
printf("____________________________________\n");
if (amenu==1)
{
fp=fopen("bankaccount.txt","a");
printf("Enter the amount to be deposit: ");
scanf("%f",&deposit);
balance=balance+deposit;
printf("You now have a balance of %f\n",balance);
printf("Quit? [Enter 'Y' to exit or 'N' to go back to account menu]: ");
scanf("%s",&quit);
fprintf(fp,"Account #%d\n",an);
fprintf(fp,"%s\n",name);
fprintf(fp,"%s\n",address);
fprintf(fp,"Account Balance: %f\n",balance);
} while(quit=='n'||quit=='N');
else if (amenu==2)
{
fp=fopen("bankaccount.txt","a");
printf("Enter the amount to be withdraw: ");
scanf("%f",&withdraw);
balance=balance-withdraw;
printf("You now have a balance of %f\n",balance);
printf("Quit? [Enter 'Y' to exit or 'N' to go back to account menu]: ");
scanf("%s",&quit);
fprintf(fp,"Account #%d\n",an);
fprintf(fp,"%s\n",name);
fprintf(fp,"%s\n",address);
fprintf(fp,"Account Balance: %f\n",balance);
} while(quit=='n'||quit=='N');
else if (amenu==3)
{
fp=fopen("bankaccount.txt","r");
if(fp==NULL)
{
printf("File doesn't exist.");
}
else
{
do
{
c=getc(fp);
putchar(c);
} while(c!=EOF);
}
}
else if (amenu>3)
{
clrscr();
printf("Quitting...");
}
}
else if (bmenu==2)
{
fp=fopen("bankaccount.txt","a");
printf("Enter Name: ");
scanf("%s",&name);
printf("Enter Address: ");
scanf("%s",&address);
an=random(10000);
printf("New account creation successful...\n");
printf("Your Account Number is: %d",an);
fprintf(fp,"Account #%d\n",an);
fprintf(fp,"%s\n",name);
fprintf(fp,"%s\n",address);
fprintf(fp,"Account Balance: %f\n",balance);
printf("Quit? [Enter 'Y' to exit or 'N' to go back to account menu]: ");
scanf("%s",&quit);
break;
} while(quit=='n'||quit=='N');
else if (bmenu>2):
{
printf("Quit");
}
getch();
}why dont you try using case switch, it will make your code easier to understand and trace for errors
while(quit=='n'||quit=='N');
where does the do start, u cant have while at the end without the initiation point
do not use if with while, while is a looping condition ( do while loop )
1. Break your program into logical units as functions, such as the code for processing menu a or menu b.
2. As suggested, instead of a great big set of if/else statements, use a switch().
3. If you want your program to be portable, don't use MS specific screen formatting functions like clrscr(). Instead, if you really want to have a nice text display, use the ncurses library instead. It works everywhere.
4. Keep your braces lined up and indent your code between them. It will help you find a lot of the bugs that exist in your program now.