Thread: How to add message at end of loop.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up How to add message at end of loop.

    Problem:
    Write a menu program that lets the user select from a list of options, and if the input is not one of the options, reprint the list
    Code:
    #include <iostream>#include <string>
    using namespace std;
    
    
    int main( )
    {
    
    
        int choice = 0;
        do
        {
    
            cout << "1: Play" << endl;
            cout << "2: Scores" << endl;
            cout << "3: Quit" << endl;
            cout << endl;
    
            cout << "Enter a number to continue: ";
            cin >> choice;
    
        }
        while(!(choice == 1 || choice == 2 || choice == 3));
    
    
        cout << "Great, You have selected an option";
    
    
        cin.ignore();
        cin.get();
    
    
        return 0;
    }
    That works fine. But then I decided what if I wanted to display a message, before it displays the options again.

    So I am thinking is my use of do whole loop the right choice?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You need to make the loop infinite and break when done.
    Code:
    int choice =0;
    while(true)
    {
        //your code
        if(condition)
            //message
        else break;
    }

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The "do-while" loop is great for this purpose. You're already testing whether the input is valid once (the argument for "while()"), so use this same validation to determine if you need to print an error message.

    Code:
    do
    {
        cout << "1: Play" << endl;
        cout << "2: Scores" << endl;
        cout << "3: Quit" << endl;
        cout << endl;
    
        cout << "Enter a number to continue: ";
        cin >> choice;
    
        // if(input_not_valid)
        //     print_error_message
    }
    while(input_not_valid);
    You can also make this argument more concise. Consider:

    Code:
    while(choice < 1 || choice > 3)
    This uses less code, does not require inversion (!) (which isn't a bad thing, but it would be more clear without it), and if you expand your menu, you only have to change one number.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up

    Here's How I came about it, its also revised code with help from: Matticus
    Code:
    #include <iostream>#include <string>
    using namespace std;
    
    
    int main( )
    {
    
    
    	int choice = 0;
    	int count = 0;
    
    
    	do
    	{
    		if (count >= 1)
    		{
    			cout << "Dummy, you entered the wrong option, try again:" << endl;
    			cout << endl;
    		}
    
    
    		cout << "1: Play" << endl;
    		cout << "2: Scores" << endl;
    		cout << "3: Quit" << endl;
    		cout << endl;
    
    
    		cout << "Enter a number to continue: ";
    		cin >> choice;
    
    
    		count++;
    	}
    	while(choice < 1 || choice > 3);
    
    
    	cout << "Great, You have selected an option";
    
    
    	cin.ignore();
    	cin.get();
    
    
    	return 0;
    }


    Would this be one of the correct ways?
    Last edited by Swadesh; 09-12-2012 at 01:42 PM.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Swadesh View Post
    Here's How I came about it, its also revised code with help from: Matticus

    Would this be one of the correct ways?[/B]

    Did you like how it worked when you tested it? If so, it's fine. If not, no.

    My preference is exit the while loop only when the quit command is entered.
    Inside the loop,
    1) test for the input (if a proper input was not given, output the message)
    2) call a function to process the given input
    3) loop returns to get another input.

    The reason I would use this is because you have an explicit QUIT command. The assumption being if you don't choose quit you want to keep inputting and processing new values.

    Do you know the switch statement?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if (count >= 1)
    {
        cout << "Dummy, you entered the wrong option, try again:" << endl;
        cout << endl;
    }
    Gotta love it when a program gets sassy with the user.

    But - what will happen if the user enters a negative number?

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    22
    Quote Originally Posted by Matticus View Post
    Code:
    if (count >= 1)
    {
        cout << "Dummy, you entered the wrong option, try again:" << endl;
        cout << endl;
    }
    Gotta love it when a program gets sassy with the user.

    But - what will happen if the user enters a negative number?
    The program seems to work fine with negative numbers,

    Code:
     while(choice < 1 || choice > 3);
    Doesn't this take care of it?

    Thanks for all the replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to understand the message loop
    By bling in forum Windows Programming
    Replies: 4
    Last Post: 08-08-2008, 09:27 AM
  2. Message loop
    By beene in forum Game Programming
    Replies: 2
    Last Post: 11-26-2007, 05:19 PM
  3. Message loop?
    By Shadowwoelf in forum Windows Programming
    Replies: 2
    Last Post: 07-21-2007, 09:58 PM
  4. message loop for DialogBox?
    By ZeroG in forum Windows Programming
    Replies: 4
    Last Post: 06-03-2004, 11:51 AM
  5. message loop VK_<key>
    By revelation437 in forum Windows Programming
    Replies: 5
    Last Post: 12-27-2003, 10:47 PM