Hello runningbear,
Since the only thing I have seen is fixing the code tags in your OP. I will cover a few points about what is there.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
int MainMenu()
{
cout << "--Main Menu--"
<< "\n1) Calculate Area"
<< "\n2) Calculate Volume"
<< "\nEnter a menu choice: ";
cin >> menuChoice;
switch (menuChoice)
{
case 1:
cout << "You chose the Calculate Area option.";
break;
case 2:
cout << "You chose the Calculate Volume option." << endl;
break;
default:
cout << "\nInvalid menu option." << endl;
return menuChoice;
}
}
|
The "cout" statement is good, although I would add 3 for exit.
Line 31 sends the input to a global variable before the switch has a chance to check the input.
The switch has no real use except to tell you what choice you made or if it was wrong.
The "default" prints an error message if the choice is invalid, but since the choice was put into a global variable it is to late and you do nothing to change the value of the global variable. The "default" case returns a global variable, which is unnecessary, because the global variable has been changed even if it is wrong. It really does not matter because you never capture the returned value or do anything with it back in "main".
The Function I showed you works much better because it only returns a valid choice. Also the variable defined in the function "menuChoice" is local to the function and after it returns its value it is destroyed until it is needed again.
In the function:
49 50 51 52 53 54 55 56 57
|
int AreaMenu()
{
if (menuChoice == 1)
cout << "\n\n--Area Menu--"
<< "\na) Rectangle"
<< "\nb) Circle"
<< "\nc) Right Triangle"
<< "\nEnter a menu choice: ";
cin >> areaChoice;
|
73 74 75
|
int AreaMenu = 0;
return AreaMenu;
}
|
The first problem is line 51. Should "menuChoice " not == 1 the only line controlled by the if statement is line 52. Line 57 is past the if statement. Which means that it will be waiting for a menu choice even if a menu is not displayed.
The If statement does not include enough nor does it have an else if anything is wrong.
Also you are setting a global variable which could have an invalid menu choice.
I left out the switch because like the "MainMenu" function it has no real use. You are checking the input to a global variable after it has been given a value even if it is wrong and there is no way to change it.
Lines 73 and 74 I have no idea what you are trying to do here, but it does not work. First you are trying to create a variable with the same name as the function. Not sure if that would even work, but it is not a good idea to define a variable with the same name as the function name.
It really does not matter because you do not use the returned value of the function.
The last part I came to realize is that in the first 2 menu functions the switch only demonstrates that you know how to use it. The "cout" statements can be placed in a switch in "main.
The menu functions should just print the menu and accept a choice followed by verifying that the choice is valid before returning to "main".
I have shown you a start to the "mainMenu" function, but now I would change the switch to an if statement to print the error message and the loop would keep going until you enter a valid choice.
Andy