简体   繁体   中英

My do while loop is not looping correctly

I might be giving more than enough but long story short I am working on an ATM machine program and I am trying to put the "switch" statement in the main function inside a loop so the user can get more transactions.

I am running into a problem where I would deposit 100 but then when I check the balance it is still at 0. I know everything else works fine but that loop is killing me, I would appreciate any help!
Don't mind all of the extra stuff it is just there to give an idea on what i am working on

int main ()
{
    char option;
    float balance ; 
    int count = 1;
    option = displayMenu();
    do
    {
        switch (option)
        {
            case 'D':
                getDeposit(balance);
                main();
                count++;
            break;
            case 'W':
                getWithdrawal(balance);
                main();
                count++;
            break;
            case 'B':
                displayBalance(balance);
                main();
                count++;
            break;
            case 'Q':
                printf("Thank you!");
            break;
                main();
        }
    } while ( count <= 5);
    return 0;
}

char displayMenu()
{
    char option;
    printf("\n  Welcome to HFC Federal Credit Union \n");
    printf("\n      Please select from the following menu: \n ");
    printf("\n  D:     Make a deposit \n ");
    printf("\n  W:  Make a withdrawal  \n ");
    printf("\n  B:  Check your account balance \n ");
    printf("\n  Q:  To quit \n ");
    scanf("\n%c" , &option);
    return option;
}

float getDeposit(float balance)
{
    float deposit;
    printf("\n Please enter the amount you want to deposit!  ");
    scanf("%f" , &deposit);
    balance += deposit;
    return balance;
}

float getWithdrawal(float balance)
{
    float withdrawal;
    printf("\n Please enter the amount you want to withdraw!  ");
    scanf("%f" , &withdrawal);
    balance -= withdrawal;
    return balance;
}

void displayBalance(float balance)
{
    printf("\n Your current balance is %f " , balance);
}

You're recursively calling main() on every iteration of the loop. Just remove this call, and you should be good to go.

You'll also need to assign the return values of your functions to balance , otherwise they won't be able to affect its value.

There are a number of issues with this code... Here are my main pointers (but not all of them, I'm just answering the question):

  1. You're calling main over and over again, for simplicity, you could consider this as restarting the application every time (except for stack issues, that I'm ignoring and other nasty side effects).

  2. You didn't initialize the balance (and friends) variables. They might contain "junk" data.

  3. You're ignoring the return values from the functions you use. If you're not using pointer, you should use assignment.

  4. Your menu printing function is out of the loop... I doubt if that's what you wanted.

Here's a quick dirty fix (untested):

int main() {
  char option;
  float balance = 0;
  int count = 1;
  do {
    option = displayMenu(); // moved into the loop.
    switch (option) {
    case 'D':
      balance = getDeposit(balance);
      count++;
      break;
    case 'W':
      balance = getWithdrawal(balance);
      count++;
      break;
    case 'B':
      balance = displayBalance(balance);
      count++;
      break;
    case 'Q':
      printf("Thank you!");
      break;
    }
  } while (count <= 5);
  return 0;
}

char displayMenu(void) {
  char option;
  printf("\n  Welcome to HFC Federal Credit Union \n");
  printf("\n      Please select from the following menu: \n ");
  printf("\n  D:     Make a deposit \n ");
  printf("\n  W:  Make a withdrawal  \n ");
  printf("\n  B:  Check your account balance \n ");
  printf("\n  Q:  To quit \n ");
  scanf("\n%c", &option);
  return option;
}

float getDeposit(float balance) {
  float deposit;
  printf("\n Please enter the amount you want to deposit!  ");
  scanf("%f", &deposit);
  balance += deposit;
  return balance;
}

float getWithdrawal(float balance) {
  float withdrawal;
  printf("\n Please enter the amount you want to withdraw!  ");
  scanf("%f", &withdrawal);
  balance -= withdrawal;
  return balance;
}

void displayBalance(float balance) {
  printf("\n Your current balance is %f ", balance);
}

Good Luck!

you haven't changed the variable in main(). you can change the loop to this:

do
{
    switch (option)
    {
    case 'D':
        balance = getDeposit(balance);
        count++;
        break;
    case 'W':
        balance = getWithdrawal(balance);
        count++;
        break;
    case 'B':
        displayBalance(balance);
        count++;
        break;
    case 'Q':
        printf("Thank you!");
        break;
    }
} while (count <= 5);

I think the main problem is the update of the switch control variable outside the loop.
Reacting to "Q" with ending is somewhat necessary... then only allowing 5 becomes unneeded.
I fixed several other things, too; and provided comments on them.
And I improved testability a little (5->6). I kept the counting, just extended to 6, in order to allow a complete test "D 100 , B, W 50, B ,Q".
Nice design by the way, to return the balance from the functions, instead of using pointers or global variable. But you need to use the return value instead of ignoring it.

/* include necessary headers, do not skip this when making your MCVE */
#include <stdio.h>

/* prototypes of your functions,
   necessary to avoid the "immplicitly declared" warnigns
   when compiling "gcc -Wall -Wextra"; which you should
 */
char  displayMenu(void);
float getDeposit(float balance);
float getWithdrawal(float balance);
void  displayBalance(float balance);

/* slightly better header of main, with "void" */
int main (void)
{
    char option;
    float balance=0.0; /* initialise your main variable */
    int count = 1;
    /* your very important update of the switch control variable has been moved ... */
    do
    {
        option = displayMenu(); /* ...here */
        /* If you do not update your switch variable inside the loop,
           then it will forever think about the very first command,
           this explains most of your problem.
         */
        switch (option)
        {
            case 'D':
                balance=getDeposit(balance); /* update balance */
                /* removed the recursive call to main(),
                   it is not needed as a solution to the problem that the program
                   always uses the first command (when updating inside the loop)
                   and otherwise just makes everything much more complicated and
                   risky.
                 */
                count++;
                break;
            case 'W':
                balance=getWithdrawal(balance); /* update balance */
                count++;
                break;
            case 'B':
                displayBalance(balance);
                count++;
                break;
            case 'Q':
                printf("Thank you!");
                /* adding a way to get out of the loop,
                   using a magic value for the count,
                   this is a mehtod frowned upon by most,
                   but it minimises the changes needed to your
                   own coding attempt.
                 */
                count=0;
                break;
        }
    } while ( (count <= 6)&&(count>0) ); /* additionally check for the  magic "Q" value
        check against count<=6, to allow testing D,B,W,B,Q  */
    return 0;
}

/* use explicitly empty parameter list for functions */
char displayMenu(void)
{
    char option;
    printf("\n  Welcome to HFC Federal Credit Union \n");
    printf("\n      Please select from the following menu: \n ");
    printf("\n  D:     Make a deposit \n ");
    printf("\n  W:  Make a withdrawal  \n ");
    printf("\n  B:  Check your account balance \n ");
    printf("\n  Q:  To quit \n ");
    scanf("\n%c" , &option);
    return option;
}

float getDeposit(float balance)
{
     float deposit;
     printf("\n Please enter the amount you want to deposit!  ");
     scanf("%f" , &deposit);
     balance += deposit;
     return balance;
}

float getWithdrawal(float balance)
{
    float withdrawal;
    printf("\n Please enter the amount you want to withdraw!  ");
    scanf("%f" , &withdrawal);
    balance -= withdrawal;
    return balance;
}

void displayBalance(float balance)
{
    printf("\n Your current balance is %f " , balance);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM