CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Posts
    2

    C++ resolved.

    Thanks everyone!
    Last edited by melissa_audrey; October 27th, 2010 at 09:31 PM. Reason: no longer need

  2. #2
    Join Date
    Feb 2009
    Posts
    252

    Re: PLEASE HELP! ATM simulation... Can't fix problem!

    A little recomendation "always align the conditions and loops it helps you to debug your own program and specially if you need to someone else to review your code.

    i did the align for you.

    your problems is that you call break after asign validate that ofcourse exits de loop and wont ask again the main question.

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main()
    {
    char option;
    double amount, total;
    double balance = 5000;
    bool validated;
    string selection;
    
    //Main Menu for ATM Options
    cout << "AUTOMATED TELLER MACHINE \n" << endl;
    cout << "ATM Main Menu \n";
    cout << "Enter letter for choice: \n";
    cout << "W (Withdrawl) \n";
    cout << "D (Deposit) \n";
    cout << "B (Show Balance) \n";
    cout << "Q (Quit) \n";
    cout <<"\n";
    cout <<"Make a selection: \n";
    
    validated = false;
    while (!validated){
       //Get the selection for the ATM machine
       cout << "What would you like today? ";
       getline(cin, selection);
       option = selection[0];
    
       if (option == 'q' || option == 'Q'){
           cout << "Goodbye" << "\n";
           break; // from loop
       }else if (option == 'w' || option == 'W'){
           cout << "What is the amount? ";
           cin >> amount;
           total = balance - amount;
           cout << "Balance is $" << total << "\n";
           continue;
       }else if (option == 'd' || option == 'D'){
           cout << "What is the amount? ";
           cin >> amount;
           total = balance + amount;
           cout << "Balance is $" << total << "\n";
           continue;
       }else if (option == 'b' || option == 'B'){
           total = balance;
           cout << "Balance is $" << total << "\n";
           continue;
       }else{
           validated = false;
           //break;
       }
    }
    
    return 0;
    }

  3. #3
    Join Date
    Sep 2010
    Posts
    66

    Re: PLEASE HELP! ATM simulation... Can't fix problem!

    It would probably be easier to use switch/case in this situation. How about something like this?

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    int main () {
    	string s;
    
    	do {
    		cout << "What would you like today?" << endl;
    		cin >> s;
    
    		switch(toupper(s[0])){
    		case 'W': 
    			cout << "What is the amount?" << endl;
    			//etc
    			break;
    		case 'Q': 
    			cout << "Goodbye";
    			return 0;
    		default: 
    			cout << "Invalid selection. Please try again." << endl;
    			break;
    		};
    	}
    		while(cin);
    
    		return 0;
    }

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: PLEASE HELP! ATM simulation... Can't fix problem!

    The problem I'm having is that I can't get this thing to loop back around and to the question
    It would be easier to have all your essential code in a separate function, while in main() you just loop it until it returns false:

    Code:
    bool ATMEmu()
    {
    . . .
    }
    
    int main()
    {
        while( ATMEnu() );
        return 0;
    }
    Last edited by Igor Vartanov; October 27th, 2010 at 04:04 PM.
    Best regards,
    Igor

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: PLEASE HELP! ATM simulation... Can't fix problem!

    The debugger shows that it is looping. Honestly, I only code Windows, so I can't tell you why, but the second time you hit getline, it already has a value in there, which when I ran it was 0. Since that wasn't one of the options, your program ended.

    FWIW, since you're using breaks and continues, your validated variable essentially has no effect.

  6. #6
    Join Date
    Oct 2010
    Posts
    2

    Re: PLEASE HELP! ATM simulation... Can't fix problem!

    Thank you everyone so much!!!

    Hey Alphadan!!! -- That fixed the problem of ending my loop, but it's printing out the question, "what would you like today" twice. Do you, or anyone, know how to fix that??? Also, I need to just put in a message if anything else is entered besides the letters I posted, that it is an error and to return it to the main menu. ANY IDEAS!!!??? Thank you all so much!

  7. #7
    Join Date
    Sep 2010
    Posts
    66

    Re: PLEASE HELP! ATM simulation... Can't fix problem!

    Quote Originally Posted by melissa_audrey View Post
    Thank you everyone so much!!!

    Hey Alphadan!!! -- That fixed the problem of ending my loop, but it's printing out the question, "what would you like today" twice. Do you, or anyone, know how to fix that???
    Huh? It prints "What would you like today?" at the beginning of every iteration through the loop. If you don't want that, put it outside the loop.

    Also, I need to just put in a message if anything else is entered besides the letters I posted, that it is an error and to return it to the main menu. ANY IDEAS!!!??? Thank you all so much!
    If you look at my code above, you will see the default entry in the switch/case. This code is executed if the value is not matched with any of the cases.

    The way that you are writing this is the reason that switch/case was invented. if...else if... else if... else if.. seems like an ugly way to do this, but it will still work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured