Help with Bank Deposit loop problem
Oct 28, 2012 at 6:51pm UTC
I can't figure out how to fix my while loop. When I enter 'n' to discontinue putting in deposit amounts, the loop still goes on. I was going to try and create a boolean variable, but I am not allowed to according to the book. Please hint me in the right direction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char userResponse;
float depositAmount = 0;
float totalDeposits = 0;
int depositCount = 0;
float averageDeposit = 0;
do
{
cout << "Enter deposit amount: " ;
cin >> depositAmount;
depositCount++;
cout << "Another? (y or n): " ;
cin >> userResponse;
userResponse = toupper(userResponse);
while (userResponse == 'Y' )
{
cout << "Enter deposit amount: " ;
cin >> depositAmount;
depositCount++;
cout << "Another? (y or n): " ;
cin >> userResponse;
userResponse = toupper(userResponse);
}
}
while (userResponse != 'Y' );
averageDeposit = totalDeposits / depositCount;
cout << fixed << showpoint;
cout << "Total deposits: " << setw(10) << setprecision(2) << totalDeposits
<< endl;
cout << "Number of deposits: " << setw(10) << setprecision(2) << depositCount
<< endl;
cout << "Average deposit: " << setw(10) << setprecision(2) << averageDeposit
<< endl;
return 0;
}
Enter deposit amount: 10
Another? (y or n): y
Enter deposit amount: 10
Another? (y or n): n
Enter deposit amount:
Last edited on Oct 28, 2012 at 6:52pm UTC
Oct 28, 2012 at 6:57pm UTC
You dont need
1 2 3 4 5 6 7 8 9 10 11
while (userResponse == 'Y' )
{
cout << "Enter deposit amount: " ;
cin >> depositAmount;
depositCount++;
cout << "Another? (y or n): " ;
cin >> userResponse;
userResponse = toupper(userResponse);
}
change
while (userResponse != 'Y' );
to
while (userResponse == 'Y' );
Oct 28, 2012 at 7:26pm UTC
Okay, now I understand why there is a semicolon after the 'while' parameters. That was puzzling me for a bit. Thanks codewalker!
Topic archived. No new replies allowed.