-
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. :confused: Should it print this line out?... :confused:
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..
-
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);
-
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 :p [/edit]
-
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..