Thread: do while loop

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    do while loop

    Hi everyone,

    I am in the process of learning C++ and having some difficulties and need some help figuring out the problem.

    The program is to balance the account by reading an initial value, the deposits and withdraws and then print them out. The problem I have is that the loop will print an extra line when it is suppose to have been terminated.

    I am using MS Visual C++...

    Here is the code.

    #include <iostream.h>

    int main()
    {
    double new_balance = 0;
    double initial_balance = 0;
    double deposit = 0;
    double withdraw = 0;
    int index = 0;

    cout <<"Please enter your account balance now: " << endl; // The initial amount
    cin >> initial_balance;
    new_balance = initial_balance;

    do
    {
    cout << "Please enter a transaction to balance: " << endl;
    cin >> index;

    if(index>0) deposit += index; //total of deposits
    else withdraw += index; //total of withdraws

    new_balance += index;
    cout << "Your new balance is: " << new_balance << "." << endl;

    }
    while (index != 0);


    cout << "The total deposit for this session is: " << deposit << "." << endl;
    cout << "The total debit for this session is: " << withdraw << "." << endl;
    cout << "Your new balance as of today is: " << new_balance << "." << endl << endl;

    return 0;
    }


    And here is what i got when I ran it..

    Please enter your account balance now:
    5000
    Please enter a transaction to balance:
    100
    Your new balance is: 5100.
    Please enter a transaction to balance:
    150
    Your new balance is: 5250.
    Please enter a transaction to balance:
    -130
    Your new balance is: 5120.
    Please enter a transaction to balance:
    -10
    Your new balance is: 5110.
    Please enter a transaction to balance:
    0
    Your new balance is: 5110. Should it print this line out?...
    The total deposit for this session is: 250.
    The total debit for this session is: -140.
    Your new balance as of today is: 5110.

    Press any key to continue


    Thanks for any help..

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    First off, please use code tags when posting code, makes it a lot easier for us to read!

    Yes, the way you have it set up, this will happen. Remember, the program isn't going to skip certain lines of code just because index is zero unless you tell it to. See if the following do while loop makes sense (kept as close to your code as possible):
    Code:
    do 
    {
    cout << "Please enter a transaction to balance: " << endl;
    cin >> index;
    
    if(index>0)
        deposit += index; //total of deposits
    else if (index<0)  // make sure you have this, otherwise a zero would increase withdraw!
         withdraw += index; //total of withdraws  
    
    new_balance += index;
    
    if (index!=0)
       cout << "Your new balance is: " << new_balance << "." << endl;
    
    }
    while (index != 0);

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Please read This link and use code tags when you post code. Yes, that line should be printed out. When the do_while loops is performed it goes through the entire loop before checking for termination. To have that line not print you would have to change your loop, most likely out of a do_while. One way would be with a for loop that runs forever, with an if statement inside it to check for termination.

    Code:
    for( ; ; ) //makes the loop run forever
    {
     cout << "Please enter a transaction to balance: " << endl;
     cin >> index;
    
     if(index>0) 
         deposit += index; //total of deposits
     elseif(index<0)
         withdraw += index; //total of withdraws
     elseif(index==0)
         break;
     new_balance += index;
     cout << "Your new balance is: " << new_balance << "." << endl;
    
    }
    This code will keep the new balance from printing when you want to quit. Try playing with break statements in your do_while code to make it do the same thing.
    [edit]There goes my helpful challenge [/edit]

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    2
    I wanted to thank both PJYelton and Draco for helping out. Both solutions works beautifully. I'll remember to use tags the next time I post something.

    Thank you..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM