Question:
C++ Menu Page question?
ana
2018-06-19 17:12:04 UTC
I have created a menu chooser program.
The program does not have errors and it runs, however, when I pick a menu option and hit enter it goes directly back to my code and closes the menu window.
I tried system ("pause") . Is there anything else I should be doing? thank you!

#include
using namespace std;

int main()
{
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked Easy.\n";
break;
case 2:
cout << "You picked Normal.\n";
break;
case 3:
cout << "You picked Hard.\n";
break;
default:
cout << "You made an illegal choice.\n";
}

return 0;

}
Three answers:
?
2018-06-20 19:41:55 UTC
I agree with EddieJ -- your program sub-menus should take you back to the original menu, and your original menu should have an EXIT choice.



However, if you're just trying to test your program, try placing an extra "cin >> choice;" (or a different char variable) just before the "return 0;" line. This will stop your program and allow you to check out the aesthetics of your menu. Then hit the Enter key to end your program.



This is just a 'quick & dirty' way to "pause" your program while creating it. You'd, of course, remove all lines like this (or at least comment them out) in the final version.
husoski
2018-06-19 20:34:28 UTC
To avoid "closing the window", you have a couple of options. Normally, this is a problem only when you're using Visual Studio's "Run with debugging" function (normal green "play button" or F5) or another IDE that doesn't insert a pause at end of run so you can see the final output before closing the console window. It's also a problem if you're trying to run a console program by double-clicking on the executable file.



For Visual Studio, you must be running on Windows, so the following OS-dependent hack will work. Add #include at the top, and then at the end of main just before "return 0;" add:



system("pause");



That won't work on any other major OS, and won't work if your program ends with some called function using the exit() function...since that will bypass the call in main(). Still, it's an easy hack to add so that you can (usually) see what the output looked like just before stopping unexpectedly.



Better is to create your VS project as a "Win32 Console Project" and then use Ctrl+F5 ("Start without debugging") to run the program. That's what I do when working in VS, along with using the "TOOLS>Customize..." menu command to add the "Start Without Debugging" button to the main toolbar.



If you started your project using the "Empty Project" template, Ctrl+F5 will probably still clear the window until you change the project. Right-click on your project name in Solution Explorer and click "Properties". Make sure "Configuration" is set to "All Configurations" on top, then on the left expand Linker> and select System under that. Use the SubSystem drop-down to choose "Console" as your subsystem type and click OK.



I think it's easier to start with a Win32 Console template and change to "Empty Project" when you create using "Application Settings", in the template dialog, before you click "Finish".
EddieJ
2018-06-19 17:29:47 UTC
You need to enclose most of your code in a loop, You also need to have a choice to exit the loop.



I haven t written the code for you because you should be able to do that yourself. Or were you looking for someone to do it for you?



If you make the changes and still have a problem, post your revised code.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...