hope to see an excellent programer :)

i want user pick can brings him back to the origin menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
int main()
{
	int choice;


	cout<<"1. go a head\n"
        "2. turn back this menu to pick again 1 or 2\n";
	cin>>choice;
	switch(choice)
	{
		case 1:cout<<"somthing";
		case 2:// at this line what should i do to turn back to 
                             cout<<"1. go a head\n"
                              "2. turn back this menu to pick again 1 or 2\n";
                                                                                                      
	}	
}


Last edited on
I'm assuming you want a way to transition back and forth from menu to another. You can accomplish this by using loops (http://www.cplusplus.com/doc/tutorial/control/):

Here's something I put together that I hope you may find helpful (it is a bit spaghetti but I hope you derive the idea then you can organize it however you like):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
using namespace std;
int main()
{
	int choice = 0; // Good practice to always initialize your variables to something

	bool menuLoop = true;
	bool mainMenu = true;
	bool optionsMenu = false;

	while (menuLoop) // The iteration statement (as long as menuLoop is true)
	{
		if (mainMenu)
		{
			cout << "1. go a head\n"
				"2. turn back this menu to pick again 1 or 2\n";
			cin >> choice;
		}
		else
		{
			std::cout << "Options menu.\n";
			std::cout << "Display a list of options here...\n";
			std::cout << "2) Back.\n"; 
			cin >> choice;
		}

		switch (choice)
		{
		case 1:
			cout << "somthing"; // Do something here (call a function, change menu states, etc...) whatever you want 

			// For example...
			optionsMenu = true; // Activate options menu state
			mainMenu = false; // Deactivate main menu state

			break; // Don't forget break statements after each case (unless you want to fall through to other cases) 

		case 2:
			optionsMenu = false; // Activate options menu state
			mainMenu = true; // Deactivate main menu state
			break;

		default: // Good to have this as a catch-all   
			break;
		}
	}
	
	return 0;
}


You should improve this by adding an enumerator to improve code readability instead of plain integers to represent your states. So something like this would be nicer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
using namespace std;
int main()
{
	int choice = 0; // Good practice to always initialize your variables to something

	bool menuLoop = true;
	bool mainMenu = true;
	bool optionsMenu = false;

	enum MenuState { OPTIONS_MENU = 1, MAIN_MENU, EXIT };

	while (menuLoop) // The iteration statement (as long as menuLoop is true)
	{
		if (mainMenu)
		{
			cout << "1. go a head\n"
				"2. turn back this menu to pick again 1 or 2\n";
			cin >> choice;
		}
		else
		{
			std::cout << "Options menu.\n";
			std::cout << "Display a list of options here...\n";
			std::cout << "2) Back.\n"; 
			cin >> choice;
		}

		switch (choice)
		{
		case OPTIONS_MENU:
			cout << "somthing"; // Do something here (call a function, change menu states, etc...) whatever you want 

			// For example...
			optionsMenu = true; // Activate options menu state
			mainMenu = false; // Deactivate main menu state

			break; // Don't forget break statements after each case (unless you want to fall through to other cases) 

		case MAIN_MENU:
			optionsMenu = false; // Activate options menu state
			mainMenu = true; // Deactivate main menu state
			break;

		case EXIT:
			menuLoop = false; // Shutdown if they enter 3
			break;

		default: // Good to have this as a catch-all   
			break;
		}
	}
	
	return 0;
}
Topic archived. No new replies allowed.