Continuing from the comments above, when you pass a variable as a parameter, it is passed by value. Meaning the function receives a copy of the variable and any changes made to the variable in the function are not being made to the original back in main()
, but instead are being made to the local copy within your function.
In C++ you have three basic ways of handling this:
pass a pointer to the variable as you would in C and update the value at that address within your function, e.g.
int DisplayMenuGetChoice (int *menu_choice) { ...
and call the function in main()
as:
DisplayMenuGetChoice(&menu_choice);
you pass a reference to menu_choice
and update the value directly within your function, e.g.
int DisplayMenuGetChoice (int& menu_choice) { ...
and call it as:
DisplayMenuGetChoice(menu_choice);
or finally don't worry about passing menu_choice
at all. You have declared your DisplayMenuGetChoice
as type int
, so simply return menu_choice;
from the function and assign the return back in main()
, e.g.
int DisplayMenuGetChoice (void)
{
int menu_choice;
...
return menu_choice;
}
in main()
,
int menu_choice = DisplayMenuGetChoice();
Putting it altogether in a couple of examples (ignoring the C solution), you could do:
Pass A Reference
int DisplayMenuGetChoice (int& menu_choice)
{
std::cout << "\nPlease select the following menu items: \n\n"
" 1. Enter Sick days.\n"
" 2. Display Something cool.\n"
" 3. End program\n\n"
"choice: ";
if (!(std::cin >> menu_choice)) {
std::cerr << "error: invalid integer input.\n";
exit (EXIT_FAILURE);
}
return menu_choice;
}
int main() {
int menu_choice;
DisplayMenuGetChoice(menu_choice);
switch (menu_choice) {
case 1: GoblinSickDays(); break;
case 2: DisplayCoolMessage(); break;
case 3: std::cout << "(bye)\n"; break;
default: std::cerr << "invalid choice.\n"; break;
}
}
Assign the Return
int DisplayMenuGetChoice (void)
{
int menu_choice;
std::cout << "\nPlease select the following menu items: \n\n"
" 1. Enter Sick days.\n"
" 2. Display Something cool.\n"
" 3. End program\n\n"
"choice: ";
if (!(std::cin >> menu_choice)) {
std::cerr << "error: invalid integer input.\n";
exit (EXIT_FAILURE);
}
return menu_choice;
}
int main() {
int menu_choice = DisplayMenuGetChoice();
switch (menu_choice) {
case 1: GoblinSickDays(); break;
case 2: DisplayCoolMessage(); break;
case 3: std::cout << "(bye)\n"; break;
default: std::cerr << "invalid choice.\n"; break;
}
}
Edit Per-Comment Re: do {...} while();
You can use any of the methods above to make the menu repeatedly redisplay until the user chooses to exit the program. For example, using any of the methods that return menu_choice
you could do:
#include <iostream>
void GoblinSickDays()
{
std::cout << "Goblin is sick.\n";
}
void DisplayCoolMessage()
{
std::cout << "Cool Message.\n";
}
int DisplayMenuGetChoice (void)
{
int menu_choice;
std::cout << "\nPlease select the following menu items: \n\n"
" 1. Enter Sick days.\n"
" 2. Display Something cool.\n"
" 3. End program\n\n"
"choice: ";
if (!(std::cin >> menu_choice)) {
std::cerr << "error: invalid integer input.\n";
exit (EXIT_FAILURE);
}
return menu_choice;
}
int main() {
int done = 0;
do {
switch (DisplayMenuGetChoice()) {
case 1: GoblinSickDays(); break;
case 2: DisplayCoolMessage(); break;
case 3: std::cout << "(bye)\n";
done = 1;
break;
default: std::cerr << "invalid choice.\n"; break;
}
} while (!done);
}
(note: while the code above simply exits on a bad input, you should handle the error (whether it is cin.fail(), cin.bad() or cin.eof()
) and so long as the error is recoverable, use std::basic_istream::ignore to remove the offending characters from stdin
before the next time the menu is displayed)
Example Use/Output
$ ./bin/goblindays3
Please select the following menu items:
1. Enter Sick days.
2. Display Something cool.
3. End program
choice: 1
Goblin is sick.
Please select the following menu items:
1. Enter Sick days.
2. Display Something cool.
3. End program
choice: 2
Cool Message.
Please select the following menu items:
1. Enter Sick days.
2. Display Something cool.
3. End program
choice: 3
(bye)
Give that a try and let me know if you have further questions.
Look things over and let me know if you have further questions.