You have MainMenuChoice initialized to 4. Then you call MainMenu which gives the user the opportunity to input their choice, but you do not pass their choice back to main. 2 options to fix it. One you can change the function from a void to an int and return MainMenuChoice. Two you can pass MainMenuChoice by reference.
Thank you so much! I find it hard to learn from other peoples code, so you showing me with my code help a lot. I appreciate it. Are there any particular advantages doing it either way?
Are there any particular advantages doing it either way?
There's nothing greatly wrong with the way you were trying but I think it's simpler to break the functions down to simple (but still useful) blocks of code. I changed the names of functions and variables to ( I think better) reflect what's going on so you end up with almost self-documenting code.
The while loop for the selection is reasonably complex so it's a good idea to separate that from the menu display which is very simple.
BTW: By breaking it down a bit you are quite often able to re-use the functions elsewhere in the program.
@kemort I fixed my code and I also added a goto function instead of doing the while statement you suggested. Does my code still come to the same result as yours. Is is okay to use goto in this situation?
The better way to do it is to pass by reference, 2 reasons. First, if you are using return you can only pass one variable back. In your case, you only have one so it works fine but if you have more than the one you have to pass by reference. Second, when you get larger programs it is better to pass by reference to save memory space. When passing a variable by reference you are actually only passing the memory address of the variable and not the variable itself which saves space in memory.
First, if you are using return you can only pass one variable back.
Or a container, or a compound object... not a good reason. Pass a reference if it is required that you have a variable to pass in and it should be modified. If that isn't required, returning a value is probably a better option.
When passing a variable by reference you are actually only passing the memory address of the variable and not the variable itself which saves space in memory.
This may be a good reason or bad reason for specific situations (although that'll tend mostly towards the bad end of the spectrum.) As far as the general case goes, it's useless.
goto function instead of doing the while statement you suggested.
goto's are there to be used I suppose but it is not good programming practice, unless you are a 'victim' of old legacy code.
The primary reason for me using the while loop is there is a bit of user input error checking going on. It's not a complete error check but does a great deal of the checking. The user gets taken around until the selection fits the menu then returns 'from whence it came' with a feasible selection.
goto's can be made to do the same but it's better to take advantage of the higher level instructions available.
Does my code still come to the same result as yours.
Testing with lots of test cases is the way to go.
Is is okay to use goto in this situation?
Bottom line - not really :(
In short, instead of 'goto', think 'return' Like the comment in the following article, I've never used it and that would be in line with the vast amount of code posted on this site.
The primary reason for me using the while loop is there is a bit of user input error checking going on. It's not a complete error check but does a great deal of the checking. The user gets taken around until the selection fits the menu then returns 'from whence it came' with a feasible selection.
@kemort So how do I do it without the goto, but still keep the main menu displayed?
So how do I do it without the goto, but still keep the main menu displayed?
Please note the following points:
1. You will never have to use a goto whatever you are trying to do with your program.
2. System pause and cls confuse any programming issues you might be having. Students love them but they are not worth the trouble and are probably the reason you ask the question.
3. I have shown you how I would do it. If I want a new menu then I just add the relevant 'display_menu()' command where I want it.
4. Alternatively have a while loop that keeps running through the selection menu until the user selects exit. Like so, you'll have to write the actual code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
while(haven't quit)
{
display_Intro();
display_Menu();
menu_selection = get_menu_selection();
if (menu_selection == 1) {
cout << "Your Chose to Play";
}
else if (menu_selection == 2) {
cout << "You chose credits.";
}
else
{
cout << "You chose to exit.";
quit the program
}
}
I'm not trying to go against what your telling me, its just my head is geared a certain way and I want to try to do what I'm thinking.
Okay, so I definitely want to clear the screen somehow when the selection isn't 1, 2, or 3. Then return to the selection screen. This is what I have right now but if you select anything other than 123 it successfully clears the screen and displays it again but thinks I've selected 3.
therefore, you're showing the prompt twice because in your main, you called the get_menu_selection() and then created a prompt when the user has selected an option. I would take out the prompt inside the main because the get_menu_selection() has error checking, so if the user enters anything that isn't 1, 2, or 3, it will display the menu again. The prompt inside main would exit even if the user's input is invalid.
Don't feel as though there is only one way and/or you have to do it my way. The best way is to try things out. And the very best way is to write pseudocode before you write any code at all. Map out on paper exactly what you want to happen.
This following code is a suggestion and maybe you can adapt it to what you are trying to achieve. You might even be able to place the cls and pause lines strategically to avoid the scrolling screen - up to you. You can combine them into a new function and place the function where you want it.
Don't feel as though there is only one way and/or you have to do it my way. The best way is to try things out. And the very best way is to write pseudocode before you write any code at all. Map out on paper exactly what you want to happen.
This following code is a suggestion and maybe you can adapt it to what you are trying to achieve. You might even be able to place the cls and pause lines strategically to avoid the scrolling screen - up to you. You can combine them into a new function and place the function where you want it.
This code makes more sense in my head. I like how once it finishes with lets say PLAY, it returns to the menu. The only thing that I would prefer is when you type in something that causes the error, is for it to tell you then press a key to clear the screen and return to the menu.