-
April 18th, 2009, 10:07 PM
#1
Do Loop
After the switch statement I have to place the request for the withdrawal amount in a do loop and keep asking for an amount until an amount less than or equal to the account balance has been entered. Negative numbers are a form of deposit. It's nott suppose to keep asking How much to withdrawl. The user should just be able to keep entering numbers until its less than or equal to the account.
Code:
// Dones, Crystal
// CSC*125 R
// ATM
#include <iostream>
using namespace std;
int main()
{
char account;
long long balance = 0;
long long withdrawal = 0;
do
{
cout << "Greetings, Mr. Paulson." << endl << endl;
cout << "Checking" << endl;
cout << "Savings" << endl;
cout << "TARP" << endl;
cout << endl;
cout << "From which of the above accounts would you like to make a withdrawal? (c/s/t)? ";
cin >> account;
cout << endl;
system ("cls");
} while (account != 'C' && account != 'c' && account != 's' && account != 'S' && account != 't' && account != 'T');
if (account == 'C' || account == 'c' || account == 's' || account == 'S' || account == 't' || account == 'T');
{
switch(account)
{
case 'c': balance = 1000;
cout << "You have $" << balance << " in your checking account." << endl;
break;
case 'C': balance = 1000;
cout << "You have $" << balance << " in your checking account." << endl;
break;
case 's': balance = 10000;
cout << "You have $" << balance << " in your savings account." << endl;
break;
case 'S': balance = 10000;
cout << "You have $" << balance << " in your savings account." << endl;
break;
case 't': balance = 700000000000;
cout << "You have $" << balance << " in your TARP account." << endl;
break;
case 'T': balance = 700000000000;
cout << "You have $" << balance << " in your TARP account." << endl;
break;
}
for (int count = 1; count <= account; count++)
{
cout << endl;
cout << "How much would you like to withdraw? $";
cin >> withdrawal;
}
cout << endl;
if (withdrawal < 0)
cout << "Sorry, negative withdrawals are not allowed." << endl;
else if (withdrawal > balance)
cout << "Sorry, that amount is not available." << endl;
else
cout << "You have $" << balance - withdrawal << " left." << endl;
cout << endl << endl;
system("pause");
return 0;
}
}
-
April 18th, 2009, 11:24 PM
#2
Re: Do Loop
Try using a do loop;
Hint: Do while withrawl is < balance ...
Last edited by CNemo; April 19th, 2009 at 01:36 AM.
-
April 19th, 2009, 01:43 AM
#3
-
April 19th, 2009, 03:26 AM
#4
-
April 19th, 2009, 08:49 AM
#5
Re: Do Loop
you have this:
Code:
for (int count = 1; count <= account; count++)
{
cout << endl;
cout << "How much would you like to withdraw? $";
cin >> withdrawal;
}
That loops 'account' times, and doesnt check at all for a valid withdawal amount.
Last edited by Amleto; April 19th, 2009 at 08:52 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|