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;

}

Recommended Answers

All 2 Replies

Clean up the leftover newline in the input stream.

The second menu is legitimate. When you typed something for scanf() to read, you also pressed [Enter], right? That [Enter] is a character too, and it is stored in the stream as '\n'. The first menu is the one you want, but because there is another character ready to be read, scanf() does not block and the loop is run to completion with an invalid option. Then it runs again, and scanf() blocks after the second menu is printed.

Long story short, scanf() does not work well when mixing formatted and unformatted input. %f is formatted input because leading whitespace is discarded and the input string is parsed into a float. %c is unformatted because no parsing is done, no leading whitespace is discarded, and the next character is read regardless of what it is.

Here is a page that describes the problems of reading single characters with scan() and code to work around it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.