Thread: Stupid Menus!!!

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    15

    Question Stupid Menus!!!

    I just can't get this menu to work!!!

    Code:
    #include <iostream>
    
    using namespace std;
    
    int Menu(int pick);
    int NewGame();
    int Online();
    int Quit();
    
    int main()
    {
        int pick=0;
        
        if (pick==0)
        cout<<Menu(pick);  
        if (pick==1)       
        cout<<NewGame();   
        if (pick==2)
        cout<<Online();     
        if (pick==3)
        cout<<Quit();   
    }
    
    int Menu(int pick)
    {
        cout<<"Welcome to SUPER GAME! \n";
        cout<<"What Would You Like To Do: \n";
        cout<<"1. New Game \n";
        cout<<"2. Online \n";
        cout<<"3. Exit \n";
        cin>>pick;
        cout<<main();
    }               
    
    int NewGame()
    {
        int pick;
        
        cout<<"This Game Is Currently Under Construction! \n";
        cin.get();
        cout<<Menu(pick);
    }
    
    int Online()
    {   
        int pick;
        
        cout<<"No Such Feature Currently Available! \n";
        cin.get();
        cout<<Menu(pick); 
    }
    
    int Quit()
    {
        return 0;
    }
    What I want it to do is it will go to the menu right? well then I want it to ask for the user to enter 1, 2, or 3 and then it will go back to MAIN. It will locate the correct function and go to it. But it will just go right back to the MENU. So this is what appears on the prompt.

    Welcome to SUPER GAME!
    What Would You Like To Do:
    1. New Game
    2. Online
    3. Quit
    X (whatever number 1-3)

    Welcome to SUPER GAME!
    What Would You Like To Do:
    1. New Game
    2. Online
    3. Quit
    X (whatever number 1-3)

    It just continues to repeat itself. Anybody know how I could make it stop doing that?

    Now I tried switching the int pick=0; up top where i state the prototypes but that doesn't work either. Anyone know what to do?

  2. #2
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    why do you
    Code:
    cout<<menu();
    ??? this is not a valid function call, unless something has changed in the language lately.
    just put
    Code:
    menu();
    same goes with the other function calls.
    also you call menu twice, that is why you are seeing the double menu.
    you call it in main, newgame, and online
    try this
    Code:
    #include <iostream>
     
    using namespace std;
     
    int Menu();
    void NewGame();
    void Online();
    int Quit();
     
    int main()
    {
    	int pick=0;
     
    	pick=Menu();
     
    	if (pick==1)	 
    	  NewGame(); //could be consolidated into a switch
    	if (pick==2)
    	  Online();	 
    	if (pick==3)
    	  Quit(); 
    }
     
    int Menu()
    {
    	int intPick=0;
     
    	cout<<"Welcome to SUPER GAME! \n";
    	cout<<"What Would You Like To Do: \n";
    	cout<<"1. New Game \n";
    	cout<<"2. Online \n";
    	cout<<"3. Exit \n";
     
    	cin>>intPick;
     
    	return(intPick);
    }			 
     
    void NewGame()
    {
     
    	cout<<"This Game Is Currently Under Construction! \n";
    	cin.get();
     
    	return;
    }
     
    void Online()
    { 
    	cout<<"No Such Feature Currently Available! \n";
    	cin.get();
     
    	return;
    }
     
    int Quit()
    {
    	return 0;
    }
    Last edited by xviddivxoggmp3; 10-23-2004 at 08:13 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    Fixed what you said to...program don't work any more X_X

    Going back to my old code anybody got a clue?

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by xviddivxoggmp3
    why do you
    Code:
    cout<<menu();
    ??? this is not a valid function call, unless something has changed in the language lately.
    thats totally valid. It will print the return value of menu(). (though it should have a capital 'M')

    @OP: Your calling main() in your menu function.... dont do that.

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    my bad.
    i have never seen it done like that before.
    i threw some code together, but i have no compiler to check if it works.
    but perspective's idea is a good one.
    Mornic_Programm
    question?
    where did you learn to code like that?
    I have never seen that type of style.
    Did you get that from a book or online?
    if so, i would like to look at it.
    i'm starting to see what you are doing.
    looks like a loop w/o a loop.
    Last edited by xviddivxoggmp3; 10-23-2004 at 08:44 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    So are you sayin, if I switch all those "if" statements to Menu and get rid of the calling of Main, it should work? Is main like, supposed to be just the starting point, which branches off to menu, and never go back to it?

  7. #7
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    no, the if statements could be consolidated into what is called a switch or case statement depending on what language you know. it will do what you want, but in my opinion in a more efficient way. main is your program code that executes other functions or methods. all of my functions return back to main. see the return statements. you technically do not need a return statement on the end of a void statement. main executes sequentially one method/function after another, but the order can be altered if needed by dicisions, loops, go to, etc. i unfortunately have been spending this semester focusing on java and vb, so i hope i'm not to far off the c++ mark.

    Code:
    //example of switch
    switch(pick)
    {
    case 1:
    	 NewGame();
    break;
     
     
    case 2:
    	 Online();
    break;
     
    case 3:
    	 Quit();
    break;
    }
    http://www.cprogramming.com/tutorial/lesson5.html
    Last edited by xviddivxoggmp3; 10-23-2004 at 08:19 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    Oh lol well in that case I'll just leave the code how it is and go onto the next lesson (switches) thanx

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    15

    Unhappy

    OK, I read the next tutorial and did the switches. I got it to work IF MainMenu didn't go back to main. Well this is the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int MainMenu(int pick);
    int NewGame();
    int Online();
    int Exit();
    int pick;
    
    int main()
    {
        cout<<MainMenu(pick);     
    }
    
    int MainMenu(int pick)
    {
           
        cout<<"Welcome to Super Game 2! \n";
        cout<<"1. New Game \n";
        cout<<"2. Online \n";
        cout<<"3. Exit \n";
        cout<<"Make A Selection: ";
        cin>>pick;
        switch (pick)
        {
            case 1:
                cout<<NewGame();
                break;
            case 2:
                cout<<Online();
                break;
            case 3:
                cout<<Exit();
                break; 
        }    
    }
    
    int NewGame()
    {
        int pick;
        
        cout<<"Sorry, Not Available In This Version! \n";
        cin.get();
        cout<<main();
    }
    
    int Online()
    {
        int pick;
        
        cout<<"Sorry, Not Available In This Version! \n";
        cin.get();
        cout<<main();
    }
    
    int Exit()
    {
        cin.ignore();
        cout<<"Thanks For Playing!";
        cin.get();
    }
    Now what I want is the code to have MainMenu not have the switch statement. I want it so that Main has the switch statement and once you pick your variable at the MainMenu, it goes to Main and sees which case fits the variable pick. This is the code I messed around with:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int MainMenu(int pick);
    int NewGame();
    int Online();
    int Exit();
    int pick;
    
    int main()
    {
          switch (pick)
        {
            case 1:
                cout<<NewGame();
                break;
            case 2:
                cout<<Online();
                break;
            case 3:
                cout<<Exit();
                break;
            default:
                cout<<MainMenu(pick);
                break; 
        }        
    }
    
    int MainMenu(int pick)
    {
           
        cout<<"Welcome to Super Game 2! \n";
        cout<<"1. New Game \n";
        cout<<"2. Online \n";
        cout<<"3. Exit \n";
        cout<<"Make A Selection: ";
        cin>>pick;
        cout<<main(); 
    }
    
    int NewGame()
    {
        int pick;
        
        cout<<"Sorry, Not Available In This Version! \n";
        cin.get();
        cout<<main();
    }
    
    int Online()
    {
        int pick;
        
        cout<<"Sorry, Not Available In This Version! \n";
        cin.get();
        cout<<main();
    }
    
    int Exit()
    {
        cin.ignore();
        cout<<"Thanks For Playing!";
        cin.get();
    }
    That code, makes the mainmenu appear every single time. No matter what number you press! It could be 9 billion and it would still go to the main menu! Anybody know how to fix it?

  10. #10
    Occasionally correct Mr.OC's Avatar
    Join Date
    Oct 2004
    Posts
    6
    Here, try this. It should work for you.

    Code:
    #include <iostream>
    
    using namespace std;
    
    //Function prototypes...
    void NewGame();
    void Online();
    void Exit();
    
    //Function declaration...
    void NewGame()
    {
    	cout << "Sorry, not available in this version!\n";
    	cin.get();
    }
    
    void Online()
    {
    	cout << "Sorry, not available in this version!\n";
    	cin.get();
    }
    
    void Exit()
    {
    	cout << "Thanks for playing!\n";
    	cin.get();
    }
    
    int main()
    {
    	int pick;
    	int done = 0;
    	while(done == 0)
    	{
    		pick = 0;
    		cout << "Welcome to Super Game 2!\n";
    		cout << "1. New game\n";
    		cout << "2. Online\n";
    		cout << "3. Exit\n";
    		cout << "Make a selection:\t";
    		cin >> pick;
    		switch(pick)
    		{
    		case 1:
    			NewGame();
    			break;
    		case 2:
    			Online();
    			break;
    		case 3:
    			Exit();
    			done = 1;
    			break;
    		}
    	}
    	return 0;
    }
    There are a few things you might want to note about this. Firstly, the functions; they were supposed to return an integer, but they didn't return anything. If you aren't returning a value, you should declare it as "void FunctionName()".

    Next; the way you're calling the functions. Instead of calling them by using "cout<<FunctionName()", just use the function name (if it returns a value, say, an integer, use this; "int value = FunctionName()").

    Now, when you call switch(), you used 'pick', which you declared earlier. However, you didn't assign a value to 'pick', so it goes straight to the default statement (as it didn't match any of the cases).

    Also, you keep calling main() to run the process again. The main() function should only need to appear once in a program, and that's when you declare it. The computer knows what to do with it.

    Instead, you should use a while loop. That'll loop through the process of asking for the user input, and then decide what to do with it.

    Hope that helps.

  11. #11
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I agree with mr.oc
    on everything he said, but below is my revision of your code.
    i appologize, but i do not have a compiler, so i can not check it for accuracy. please someone compile it to tell me what i did wrong.
    [edit1]and the system would allow my spacing.
    argg. i will try to fix on minute. it didn't like my code tags for some reason.[/edit1]
    [edit2]finally with spacing and comments. btw our editors for this forum are very frustrating.[/edit2]

    Code:
    #include <iostream.h> 
    #include<conio.h>
    #include<stdio.h>
     
    int MainMenu();
    void NewGame();
    void Online();
    void Exit();
     
    int main()
    {
       int sentinal=0;
     
      while(sentinal!=-1)
      {
    	 switch (MainMenu())	//returns the selected menu option
    	 {
    		case 1:
    		 NewGame();
    		break;
     
    		case 2:
    		 Online();
    		break;
     
    		case 3:
    		 sentinal=-1;
    		 Exit();
    		break;
    		} 
    	 }
      }
    return 0;
    }
     
    int MainMenu()
    {
    	int pick=0;
     
       cout<<"Welcome to Super Game 2! "<<endl; //endl clears buffer & newline 
       cout<<"1. New Game"<<endl;
       cout<<"2. Online"<<endl;
       cout<<"3. Exit"<<endl;
       cout<<"Make A Selection: ";
     
       return pick;
    }
     
    void NewGame()
    {
       cout<<"Sorry, Not Available In This Version! \n"<<endl;
       getch();		  //get character from keyboard to continue
    }
     
    void Online()
    {
       cout<<"Sorry, Not Available In This Version! \n"<<endl;
       getch();
    }
     
    void Exit()
    {
       cout<<"Press any key to EXIT program!"<<endl;
       getch();
       exit(0);			 //exit program normally
    }
    Last edited by xviddivxoggmp3; 10-24-2004 at 04:18 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  12. #12
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    i give up on that post, every time i go into it to correct something the spacing would be lost or the code tags wouldn't work, or something else went wrong. if you have any questions i will just repost. that post was cursed.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 menus and resources help
    By firestorm in forum Windows Programming
    Replies: 24
    Last Post: 04-12-2005, 01:23 PM
  2. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  3. Menu's
    By Benzakhar in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2004, 10:13 PM
  4. Incredibly Stupid People
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-09-2003, 04:12 PM
  5. Replies: 6
    Last Post: 08-04-2003, 12:22 AM