Like Tree3Likes
  • 1 Post By manasij7479
  • 1 Post By Matticus
  • 1 Post By WaltP

How to add message at end of loop.

This is a discussion on How to add message at end of loop. within the C++ Programming forums, part of the General Programming Boards category; Problem: Write a menu program that lets the user select from a list of options, and if the input is ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You need to make the loop infinite and break when done.
    Code:
    int choice =0;
    while(true)
    {
        //your code
        if(condition)
            //message
        else break;
    }
    Swadesh likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    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.
    Swadesh likes this.

  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,157
    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?
    Swadesh likes this.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    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, 04: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, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21