What should I put in the main function ?

This is a very complicated, I'm trying to build a main menu and a sub menu, the users choose options in the main menu, they will go to the sub menu (all the options in the main menu have the same sub menu), in the sub menu they can choose the options, they decide whether to continue or not, they can also go back to the main menu. Until now, I don't know what to put in the main function.
Can you guys help me how to solve this ? Thank you

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
using namespace std;

int MainMenu()
{
	cout << "============== Main Menu ==============" << endl;
	cout << "1. Selection Sort" << endl;
	cout << "2. Quick Sort" << endl;
	cout << "3. Quit" << endl;
	int mainOptions = 0;
	while (mainOptions < 1 || mainOptions > 3)
	{
		cout << "Your option: ";
		cin >> mainOptions;
	}
	return mainOptions;
}

// All the options in the main menu have the same sub menu
int SubMenu()
{
	cout << "============== Sub Menu ==============" << endl;
	cout << "1. Print Array" << endl;
	cout << "2. Sort Ascending" << endl;
	cout << "3. Go back to main menu" << endl;
	int subOptions = 0;
	while (subOptions < 1 || subOptions > 3)
	{
		cout << "Your option: ";
		cin >> subOptions;
	}
	return subOptions;
}

// Let users continue with the sub options in the sub menu
bool again()
{
	cout << "Again? ";
	char c;
	cin >> c;
	return c == 'Y' || c == 'y';
}

// Print Array
void sub_option_1()
{
	do {
		// do something
	} while (again);
}

// Sort Ascending
void sub_option_2()
{
	do {
		// do something
	} while (again);
}

int main()
{
	bool quit = false;
	while (!quit)
	{
		// What should I put here ?
	}

	cout << "bye" << endl;
}
Last edited on
The main() function is the "entry point" of your program, i.e. the first function that will be called by the C Runtime when your program starts. Once the main() function returns, your program will terminate.

Be aware: All other functions only run, if they are called directly from the main() function, or if they are called indirectly from another function that was called from main() before.

So, probably the first thing you want to do in your main() function is calling the MainMenu() function. Once MainMenu() has returned, you can decide what to do next, depending on the return value of MainMenu().

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
int main()
{
	/* loop until we are done [main menu loop] */
	for(;;)
	{
		/* display main menu */
		const int mainOption = MainMenu();
		if (mainOption == 3)
		{
			break; /* exit from main loop, if user has selected "Quit" */
		}

		/* loop until we are done [sub-menu loop] */
		for(;;)
		{
			/* now display sub menu */
			const int subOption = SubMenu();
			if (subOption == 3)
			{
				break; /* exit from sub-menu loop, if user has selected "Go back to main menu" */
			}

			/* do whatever needs to be done, based on user's selection! */
			switch (subOption)
			{
			case 1:
				cout << "User has selected: Print Array" << endl; /* TODO: implement! */
				break;
			case 2:
				cout << "User has selected: Sort Ascending" << endl; /* TODO: implement! */
				break;
			default:
				abort(); /* this is not supposed to happen! */
			}
		}
	}

	cout << "bye" << endl;
}
Last edited on
Maybe something 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using std::cout;
using std::cin;

int getOpt(const char* prm, int mn, int mx) {
	int opt {};

	//NOTE does not deal with non-integer input!
	do {
		cout << prm;
		cin >> opt;

	} while ((opt < mn || opt > mx) && (cout << "Invalid option\n"));

	return opt;
}

int MainMenu() {
	cout << "============== Main Menu ==============\n";
	cout << "1. Selection Sort\n";
	cout << "2. Quick Sort\n";
	cout << "3. Quit\n";

	return getOpt("Your option: ", 1, 3);
}

int SubMenu() {
	cout << "============== Sub Menu ==============\n";
	cout << "1. Print Array\n";
	cout << "2. Sort Ascending\n";
	cout << "3. Go back to main menu\n";

	return getOpt("Your option: ", 1, 3);
}

int main() {
	while (true) {
		const auto men { MainMenu() };

		if (men == 3)
			break;

		while (true) {
			const auto sub { SubMenu() };

			if (sub == 3)
				break;

			switch (men) {
				case 1:
					switch (sub) {
						case 1:
							// Selection sort print
							break;

						case 2:
							// Selection sort
							break;
					}
					break;

				case 2:
					switch (sub) {
						case 1:
							// Quick sort print
							break;

						case 2:
							// Quick sort
							break;
					}
					break;
			}
		}
	}

	cout << "bye\n";
}

Last edited on
Thanks guys, I have figured it out
for the first time I saw for(;;) instead of while()
Interesting :)
Last edited on
for (;;) is old-style C code for an infinite loop. Every part of a for-loop is optional. If there is no condition specified, then no condition is evaluated so the for-loop loops forever unless within the body there is exit code (eg break, return).

So, for(;;) is the same as while(true) ?
Last edited on
Yes.
Many people prefer for(;;), because it definitely has no condition to be checked and therefore unambiguously marks an "infinite" loop. Meanwhile, while(true) does have a condition that (in theory) needs to be checked after each iteration of the loop, even though that condition clearly is always going to be true. Consequently, for(;;) might be slightly faster, at least in theory. But I'm pretty sure that, with any "modern" compiler, there will be no difference between for(;;) and while(true) in the generated machine code...

I still prefer for(;;) for readability.

Writing while(true) instead of for(;;) feels like writing if (expr == true) instead of just if (expr) ;-)
Last edited on
Topic archived. No new replies allowed.