while loop help

Discussion in 'C' started by nate201, Sep 3, 2010.

  1. nate201

    nate201 New Member

    Joined:
    Sep 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have to make a program for class to maintain a bank balance. The problem I am having is I can make all my deposits then my withdrawals, but i cant return to deposit after i make a withdrawal. How can I make it with only one while loop so i can switch between the two without any problems?
    Code:
    int main()
    {
            double total = 0.0;
    
            double x;
    
            char resp;
    
            explanation();
    
            printf("BANK ACCOUNT PROGRAM\n");
            printf("--------------------\n\n");
    
            printf("Please Enter the old balance: ");
            scanf("%lf%*c",&total);
            printf("Enter the transactions now.\n");
            printf("Enter an \"F\" for the transaction type when you are finished.\n\n");
            printf("Transaction Type (D=Deposit, W=Withdrawal, F=Finished): ");
            scanf("%c%*c",&resp);
    
            while(resp=='D') 
            {
                    printf("Amount: ");
                    scanf("%lf%*c",&x);
                    
                    total = total + x;  
    
                    printf("Transaction Type (D=Deposit, W=Withdrawal, F=Finished): ");
                    scanf("%c%*c",&resp);
            }
    
            while(resp=='W')
            {
                    printf("Amount: ");
                    scanf("%lf%*c",&x);
    
                    total = total - x;
    
                    printf("Transaction Type (D=Deposit, W=Withdrawal, F=Finished): ");
                    scanf("%c%*c",&resp);
            }
    
            while(resp=='F')
            {
                    printf("Your ending balance is: $%8.2lf\n",total);
                    printf("Program Ending");
            }
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice