Do Loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Do Loop

  1. #1
    Join Date
    Apr 2009
    Posts
    2

    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;
    }
    }

  2. #2
    Join Date
    Apr 2009
    Posts
    57

    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.

  3. #3
    Join Date
    Apr 2009
    Posts
    57

    Re: Do Loop

    See my reply above.

  4. #4
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994

    Re: Do Loop

    [ Moved thread ]

  5. #5
    Join Date
    Apr 2008
    Posts
    726

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center