I am trying to write a little program with a do while loop and if statements.
I'm having trouble getting this to work the way I want. When I run the program, it produces two menus after making a selection. I would like it to produce one menu after making a selection
the output is -----
Welcome to ABC bank ATM!
d: Deposit
w: Withdraw Amount
b: Check Balance
e: Exit the ATM program
Now Enter Your Choice: b
Your Balance is: 10000.00
d: Deposit
w: Withdraw Amount
b: Check Balance
e: Exit the ATM program
Now Enter Your Choice:
d: Deposit
w: Withdraw Amount
b: Check Balance
e: Exit the ATM program
Now Enter Your Choice:
---
As you can see after making my selection, the program produces two menus. Im sure I have the set up for my do while loop wrong, just cant seem to be able to pinpoint where.
Advice would be appreciated. Thanks.
This is the source code.
---
#include<stdio.h>
main()
{
//declare and intialize variables
char cChoice = '\0';
float balance = 10000.00;
float amount = 0.00;
system("clear"); //a command to clear the screen
//introductory messages to user and get the user choice for banking
fprintf(stdout,"\n\tWelcome to ABC bank ATM!\n");
do {
//introductory messages to user and get the user choice for banking
fprintf(stdout,"\n\td:\tDeposit\n\tw:\tWithdraw Amount\n\tb:\tCheck Balance\n\te:\tExit the ATM program\n");
fprintf(stdout,"\tNow Enter Your Choice:\t");
fscanf(stdin,"%c",&cChoice);
//Go further only IF the user enters a valid input
//choice that is, 'd' or 'b' or 'w'
if ( cChoice == 'd' || cChoice == 'w' || cChoice == 'b')
{
//if user choice is 'b' for checking balance do this..
if (cChoice == 'b')
fprintf(stdout, "\n Your Balance is: \t\t\t\t%.2f\n\n",balance);
//if user choice is 'd' for deposit do this..
if (cChoice == 'd')
{
fprintf(stdout,"\n\n Enter the amount to deposit:\t\t\t");
fscanf(stdin, "%f", &amount);
balance = amount + balance;
fprintf(stdout,"\n\n Updated Balance is %.2f\n\n",balance);
sleep(1);
system("clear");
}
//if choice is 'w' for withdraw amount do this..
if(cChoice == 'w')
{
fprintf(stdout,"\n Enter the amount to withdraw:\t\t\t");
fscanf(stdin, "%f", &amount);
//nested if else, that allows withdrawl only if there is enough balance
if(balance<amount)
fprintf(stdout,"\nInsufficient Funds\n\n");
else
{
balance = balance - amount;
fprintf(stdout,"\n\n Updated Balance is %.2f\n\n", balance);
sleep(1);
system("clear");
}
} //choice entered is 'w' ends here
}
}
while ( cChoice !='e' );
return 0;
}