How to go back to menu

This is a discussion on How to go back to menu within the C++ Programming forums, part of the General Programming Boards category; I have c++ code and i want to now which code i can use to gå back to menu , ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    25

    How to go back to menu

    I have c++ code and i want to now which code i can use to gå back to menu , i mean if i wrot click any kee to gå back to menu (which is up on my code ), how can i do this

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    ... what?! ...

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You really are not providing enough details for us to give any kind of a useful response. It sounds like this is a console program (no windows - just text). If that's the case, you probably want to clear the screen (see the FAQ for details on how to do this), and then reexecute the code you used to display the menu. From here on, I've had to take wild guesses at how your program might be set up.

    If your menu is in a loop, just 'continue' to start another iteration of the loop. If your menu is in a separate function, you need to call that function or end the function you're in now. Or you could use a 'goto', but this is considered extremely bad style and if you feel the need to use it, there's a good chance your program as a whole needs redesigning.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Hi
    Here is the code and i want from the last text to go back to the menu which is the first block starting with ENIGMA 2005.

    Code:
    #include <iostream>
    using namespace std;	// 
    
    int main ()				// 
    {
    	cout << "\ENIGMA 2005 ";   // 
    	cout << '\n';
    
    	int tal1;		// 
    	int tal2;		// 
    	int tal3;		// 
    	cout << "\What are you doing? \n";		// meddelande
        cout << "(1) scapp a message \n"; 		// 
     	cout << "(2) decrypt message \n";  		// Val av material Sten
    	cout << "Choose: \n";  		// Val av material Sten	
    	int material;
        cin >> material;
    	if (material == 1)
    {
        cout << "Write a message \n";
    	cout << '\n';
    	cout << "Choose keycode (2-5): \n";
    	cin >> material;	
    	
    }
    
    	if (material == 3)
    {
    	cout << "Programm has genererat keycode: \n";  
    	cout << "8, 1, 2 \n"; 
    	cout << "Write a message you want crypted: \n";
    	cin >> material;
    	cout << "Your crypt message: \n";
    	cout << "MOITBPL: \n";
    	cout << " << Click any key to gå back to menu  >> \n";
    	cin >> material;
    	
    }
    	return 0;			// 
    }

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The only way to do it with that code setup is to use a "goto" - but you know what that means - it means you need to redesign your program. I would set up different functions for displaying each menu - like "DisplayMainMenu()", "ClearScreen()". And you probably want to set up a loop that ends when the user selects exit - so that you can keep slecting different options from the main menu, returning, doing aother task, etc...

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Can you help me to redesign my program

  7. #7
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    hey dude there is a lot of mistakes in your codes!
    consider revising and see what output u want and what u have actually written
    To name some of the mistakes:
    1) the variables tal1, tal2,tal3 are never being reference anywhere in your code.
    2) bad indentation and there are too many unused // in ur codes
    3) what does tis mean:
    Code:
    cout << " << Click any key to gå back to menu  >> \n";
    It shud rather be written like this:
    Code:
    cout << "Click any key to gå back to menu \n";
    4) your "if-statements" are not complete, that is you are asking the user to enter something, BUT what are u doing with that "something" ?? nothing is being done actually.


    And to reply to your question:
    just put your whole code in infinite a loop, perhaps like :

    Code:
    int main ()
    {
    
    	do
    	{
    
    		cout << "\nENIGMA 2005 ";
    		cout << '\n';
    
    		/* all your code body goes in here*****************
    		***********************************************
    
    		if (material == 3)
    		{
    			cout << "Programm has genererat keycode: \n";
    			cout << "8, 1, 2 \n";
    			cout << "Write a message you want crypted: \n";
    			cin >> material;
    			cout << "Your crypt message: \n";
    			cout << "MOITBPL: \n";
    
    			cout << "Click any key to gå back to menu\n";
    			cin.get();
    
    		}
    		return 0;
    		***************************************************/
                      
    	}while(1);		//loop infinetly so that u get back to menu each time
    
    
    }
    But try to put a validation, so that the user can exit the program when he wishes to, perhaps u can use 2menus to do this....

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Hi
    I tried but i can go back to the menu , please can you help me what is wrong.

    Code:
    int main ()
    {
    
    	do
    	{
    
    		cout << "\nENIGMA 2005 ";
    		cout << '\n';
    
    		cout << "\What are you doing? \n";		// meddelande
        cout << "(1) scapp a message \n"; 		// 
     	cout << "(2) decrypt message \n";  		// Val av material Sten
    	cout << "Choose: \n";  		// Val av material Sten	
    	int material;
        cin >> material;
    	if (material == 1)
    {
        cout << "Write a message \n";
    	cout << '\n';
    	cout << "Choose keycode (2-5): \n";
    	cin >> material;	
    	
    }
    
    		if (material == 3)
    		{
    			cout << "Programm has genererat keycode: \n";
    			cout << "8, 1, 2 \n";
    			cout << "Write a message you want crypted: \n";
    			cin >> material;
    			cout << "Your crypt message: \n";
    			cout << "MOITBPL: \n";
    			cout << "Click any key to gå back to menu\n";
    			cin.get();
    
    		}
    		return 0;
                      
    	}while(1);		//loop infinetly so that u get back to menu each time
    }

  9. #9
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    It will repeat infinitely with that code if you put the return 0 outside of the loop.

  10. #10
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    i have added something in it..here is the code...but the prob is that the program will lopp forever.....but just tell what u want ur program to be and i'll try to adjust it! till then here is the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {	
    
    	do
    	{
    
    		cout << "\nENIGMA 2005 ";
    		cout << '\n';
    
    		cout << "\nWhat are you doing? \n";		// meddelande
    		cout << "(1) scapp a message \n";		// 
    		cout << "(2) decrypt message \n";		// Val av material Sten
    		cout << "Choose: \n";	
    		// Val av material Sten
    		int material;
    		cin >> material;
    
    		if (material == 1)
    		{
    			cout << "Write a message \n";
    			cout << '\n';
    			cout << "Choose keycode (2-5): \n";
    			cin >> material;
    			continue;
    
    		}
    
    		if (material == 3)
    		{
    			cout << "Programm has genererat keycode: \n";
    			cout << "8, 1, 2 \n";
    			cout << "Write a message you want crypted: \n";
    			cin >> material;
    			cout << "Your crypt message: \n";
    			cout << "MOITBPL: \n";
    			cout << "Click any key to gå back to menu\n";
    			cin.get();
    			continue;
    
    		}
    
                      
    	}while(1);//loop infinetly so that u get back to menu each time
    
    return 0;
    }

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {	
    
    	do {
    
    		cout << "\nENIGMA 2005" << endl;
    
    		cout << "\nWhat are you doing?" << endl;		// meddelande
    		cout << "(1) scapp a message" << endl;		// 
    		cout << "(2) decrypt message" << endl;		// Val av material Sten
                    cout << "To exit type: /quit" << endl;
    		cout << "Choice: \n";	
    		// Val av material Sten
    		string material;
    		cin >> material;
    
    		if (material == "1") {
    			cout << "Write a message \n";
    			cout << '\n';
    			cout << "Choose keycode (2-5): \n";
    			cin >> material;
    			continue;
    
    		} else if (material == "3") {
    			cout << "Programm has genererat keycode: \n";
    			cout << "8, 1, 2 \n";
    			cout << "Write a message you want crypted: \n";
    			cin >> material;
    			cout << "Your crypt message: \n";
    			cout << "MOITBPL: \n";
    			cout << "Click any key to gå back to menu\n";
    			cin.get();
    			continue;
    		} else if(material == "/quit") {
                       break;
                    } else {
                       cout << "Invalid entry." << endl;
                    }
                      
    	} while(1); //loop infinetly so that u get back to menu each time
    
    return 0;
    }
    Theres the quit ability, and it should be 'else if', not 'if', because you can only choose one.. right? Making them all if statements will result in problems if you add 'else' to any of them. Added an else statement to it also, if the user enters nothing.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    NEVER EVER USE GOTO
    use a loop
    heres an example its a calculator i made
    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    int x;
    int y;
    int input;
    do{
    cout<<"1. multiply\n";
    cout<<"2. devide\n";
    cout<<"3. addition\n";
    cout<<"4. subtraction\n";
    cout<<"please provide the number listed\n";
    cin>> input;
    switch ( input )
    {
    case 1:
    cout<<"please provide 2 numbers to be multiplied\n";
    cin>> x >> y ;
    cin.ignore();
    cout<<( x*y );
    cin.get();
    break;
    case 2:
    cout<<"please provide 2 numbers to be devided\n";
    cin>> x >> y ;
    cin.ignore();
    cout<<( x/y );
    cin.get();
    break;
    case 3:
    cout<<"pls provide 2 numbers to be added\n";
    cin>> x >> y ;
    cin.ignore();
    cout<<( x+y );
    cin.get();
    break;
    case 4:
    cout<<"pls provide 2 numbers to be subtracted\n";
    cin>> x >> y ;
    cin.ignore();
    cout<<( x-y );
    cin.get();
    break;
    }
    }while(input!=4);
    }

    and you have good english for someone forein another forein kid at my school has been here for 3 years and he still can barely put together the most basic sentence
    Last edited by lilhawk2892; 09-26-2005 at 06:04 AM.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    hahahahahahahahahahahahhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhh i need to learn more and sydney won the grand finale.

  14. #14
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by lilhawk2892
    NEVER EVER USE GOTO

    and you have good english for someone forein another forein kid at my school has been here for 3 years and he still can barely put together the most basic sentence
    Settle down, read the article on goto. Its obviously not appropriate for this situation, but it does have 1/10000 cases where it is appropriate, and no one said it was either.

    Its foreign, by the way.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    I want to add to the end of my code this block:
    choose siffra nr 1:
    choose siffra nr 2:
    choose siffra nr 3:
    and it must give the users possibility to write up the nummbers they choose. Thanks alot
    Code:
    include <iostream>
    
    using namespace std;
    
    int main ()
    {	
    
    	do {
    
    		cout << "\nENIGMA 2005" << endl;
    
    		cout << "\nWhat are you doing?" << endl;		// meddelande
    		cout << "(1) scapp a message" << endl;		// 
    		cout << "(2) decrypt message" << endl;		// Val av material Sten
                    cout << "To exit type: /quit" << endl;
    		cout << "Choice: \n";	
    		// Val av material Sten
    		string material;
    		cin >> material;
    
    		if (material == "1") {
    			cout << "Write a message \n";
    			cout << '\n';
    			cout << "Choose keycode (2-5): \n";
    			cin >> material;
    			continue;
    
    		} else if (material == "3") {
    			cout << "Programm has genererat keycode: \n";
    			cout << "8, 1, 2 \n";
    			cout << "Write a message you want crypted: \n";
    			cin >> material;
    			cout << "Your crypt message: \n";
    			cout << "MOITBPL: \n";
    			cout << "Click any key to gå back to menu\n";
    			cin.get();
    			continue;
    		} else if(material == "/quit") {
                       break;
                    } else {
                       cout << "Invalid entry." << endl;
                    }
                      
    	} while(1); //loop infinetly so that u get back to menu each time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  2. dos menu....
    By Drake in forum Game Programming
    Replies: 10
    Last Post: 01-15-2006, 09:16 AM
  3. Lost BIOS menu options?
    By PJYelton in forum Tech Board
    Replies: 3
    Last Post: 11-14-2004, 07:23 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM

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