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
#include <iostream>
usingnamespace 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;
}
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().
int main()
{
/* loop until we are done [main menu loop] */
for(;;)
{
/* display main menu */
constint 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 */
constint 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;
}
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).
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) ;-)