0

I am making a bank program with functions.1 called displaymenu that produces a menu d(make deposit),w(make withdraw),b(check balance) and
q(quit).2nd function is getdeposit which accepts balance as parameter and asks user for amount they want to deposit then add amount
to balance and return it to main function.3rd getwithdrawal same parameter (balance and subtract amount and return result)
last displaybalance it also accepts balance as parameter and displays currentbalance.then use a switch statement to select which function
to choose based on selection if they choose to quit respond thankyou.I have my switch statements but I have to implement a do while loop that asks the user if they want another tranaction. The screen asks if i want another transaction but instead of doing it it exits the program. can someone look over my code and suggest a fix?

  #include <stdio.h>

            char displaymenu();
            float getdeposit(float amount,float balance);
            float  getwithdrawal(float amount,float balance);
            float displaybalance(float balance);

               int main()
            {

                char  choice;
                char transaction='y';
                float deposit;
                float withdrawal;
                float balance;
                float amount;

                do   {

                 choice=displaymenu();

                 switch (choice)
              {

                     case 'd':case 'D':
                        printf("How much would you like to deposit?\n");
                        deposit=getdeposit(amount,balance);
                        break;

                     case 'w':case 'W':
                        printf("How much would you like to withdraw?\n");
                       if (((withdrawal== 0) || (withdrawal== -1))){
                           printf("You have insufficient funds");
                       }
                       else 
                        {    
                             withdrawal=getwithdrawal(amount,balance);
                             break;
                        }

                     case 'b':case 'B':
                         printf("Checking your account balance\n");
                         balance=displaybalance(balance);
                         break;

                     case 'q':case 'Q':
                       printf("Quit\n");
                       printf("Thankyou");
                        break;

                       default:
                       printf("Invalid Choice\n");

               }
                   printf("\n\n\n DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): \n");
                        scanf("%c", &transaction);

              } while 

                    (transaction == 'y'|| transaction == 'Y');

                return 0;
             }

                    char displaymenu()
                   {

                    char choice;

                      printf("Welcome to Federal Credit Union!\n");
                      printf("Please select from the following menu\n");
                      printf("d. Make a deposit\n");
                      printf("w. Make a withdrawal\n");
                      printf("b. check balance\n");
                      printf("q. Quit\n");
                      scanf ("%c",&choice);
                      return choice;

                }

                    float getdeposit(float amount,float balance)
                {

                     float deposit;
                     deposit=amount+balance;
                     scanf ("%f",&deposit);
                     return deposit;

                  }

                   float  getwithdrawal(float amount,float balance)
               {

                   float withdrawal;
                   withdrawal=amount-balance;
                   scanf ("%f",&withdrawal);
                   return withdrawal;
               }

                   float displaybalance(float balance)
                {

                    printf("Your balance is %.2f\n",&balance);

                 }
2
Contributors
1
Reply
17
Views
3 Weeks
Discussion Span
Last Post by Reverend Jim
0

What is happening is that when you read a char for "do you want to do another transaction" it is returning the next character from the last scanf. You can fix this by making sure that unused characters are flushed. One way to do this is to add

void flush_stdin()
{
    int c;

    while ((c = fgetc(stdin)) != '\n' && c != EOF);
}

and call it after every scanf. Now, this may not be the best way (my C is quite rusty). I have a few m ore comments...

In the following:

case 'w':case 'W':
    printf("How much would you like to withdraw?\n");
    if (((withdrawal== 0) || (withdrawal== -1))){
        printf("You have insufficient funds");
    }

you haven't done any input for withdrawal.

In the following, balance needs to have an initial value.

case 'b':case 'B':
    printf("Checking your account balance\n");
    balance=displaybalance(balance);
    break;

Same thing for amount here

case 'd':case 'D':
    printf("How much would you like to deposit?\n");
    deposit=getdeposit(amount,balance);
    break;

I strongly suggest you adopt a consistent formatting style. You are using both the

statement {
    statement
}

form as well as

statement
{
    statement
}

form. Use one or the other. Not both.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.