Thread: Menu problem.

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    65

    Menu problem.

    Hello.

    creating a simple menu so the player can insert what difficulty he would like to play at.

    Code:
    int main()
    {
    	char choice = 'y';
    	do
    	{
    	enum difficulty{Easy = 1, Medium, Hard};
    	cout << "\t\tWelcome to my Menu.\n\n";
    	cout << "1 - Easy\n";
    	cout << "2 - Normal\n";
    	cout << "3 - Hard\n\n";
    	
    
    	int choose;
    	cout << "Choice: ";
    	cin >> choose;
    
    	switch(choose)
    	{
    	case 1:
    		cout << "You picked the difficulty: Easy";
    		break;
    	case 2:
    		cout << "You picked the difficulty: Normal";
    		break;
    	case 3:
    		cout << "You picked the difficulty: Hard";
    	            break;
    	default:
    		cout << " You can only choose from 1-3, try again? (Y/N)\n: ";
    		cin >> choice;
    		system("cls");
    	
    	}
    	}
    	while(choice == 'y');
    
    	cout << "Okey then, bye.";
    
    		getch();
            return 0;
    }
    How come the program reloops the program in an instant after I have picked 1-3?
    works like it should do when I pick 4.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, in case 1, it prints the difficulty line, and then ... well, that's it; that's all you put in the case, so that's all that happens.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    What did u just say? ^^

    If I pick 1, then it will say, You choose the difficulty: Easy
    and then the break; should.. break out.

    where is the endless looping comming from? ^^

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    From the do...while loop? break only breaks out of the switch, it doesn't break out of the outer loop too (you wouldn't want it to, anyway, 99999999 times out of 100000000).

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Well, im out of ideas, can u give me a hint to solve my problem?

    I want the program to end if I pick 1-3.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    or not end, display the message, then wait for the getch() and then quit.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you don't want it to loop, don't put it in a loop. Why have the do...while if you only want it to happen once? Or, if you want the loop for "not 1-3" case, then your loop condition needs to be based on the value of choose.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Of course it works when you choose 4 cause it snaps to the default one. If you choose 1-3 it will display, then you never give the user the option to say Y or N. So... choice is always equal to Y until you choose something other than 1-3.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    ofc. I want the program to loop, otherwise I wouldnt put in a (y/n) question.

    but now it seems that everything loops.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Just put choice = 'n'; at the beginning of the loop. You only set it to 'y' in the default case and it would loop again.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Well if I do that, the compiler will most likley whine about that choice have been initialized more than once, or something like that.

    Im so bad at fixing errors ^^

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Im so stuck, gah, getting irritated.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Assignment is not initialization - there is no such thing as initializing something twice. You could even leave it uninitialized initially and the compiler would still see that you assign it a value two lines down.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Now when I put in the choice = 'n'; the code works atleast better.

    now another problem came up, now is the line cout << "Okey then, bye.";
    showing everytime.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, too bad now there's no way to tell after the loop whether anything was picked or not. And when you think about it, there's also no way to tell what difficulty you chose, since that variable is also local to the do-loop (should you want to do more than that after selecting difficulty). Perhaps move the choose variable out of the loop too (and rename it to something more descriptive like difficulty) and initialize it to 0. Later you can check if it is still 0 (no difficulty was chosen).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Simple Menu Problem
    By DanC in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2006, 01:33 PM
  3. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 02:10 AM
  4. Window - Menu problem...
    By FromHolland in forum Windows Programming
    Replies: 1
    Last Post: 02-26-2004, 03:49 PM
  5. MDI and MENU Problem
    By Marc in forum Windows Programming
    Replies: 3
    Last Post: 02-21-2004, 06:59 PM