Hello runningbear,
When I tried to compile the program there was some errors that need to be addressed.
From the beginning: Your prototypes are good in that you have 1 for every function, but after that some are wrong.
The "int getData();" works if you need this function to return a value, but maybe the assignment is ment to pass a variable by reference?
The "void displayMenu(int & val);" Works and has the right parameter.
The "void processMenuChoice();" 1st the prototype needs to match the function definition. And this function needs 2 parameters.
"void isPosNeg();" and "void isOddEven();" I believe these are wrong as I think they need at least 1 parameter, but not important yet to deal with.
At this point debugging the code should be done in much the same way as you should have written the code, i.e., in small steps. Step 1 would be dealing with the "displayMenu" function. You need to get that working properly before you go to step 2 the "processMenuChoice" function. Without a valid choice from "displayMenu" there is not much point in the "processMenuChoice" unless you want to add more code than you need.
Replace you "main" function with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int main()
{
int menuChoice = 0;
std::cout << "\n B4 the function call the value of menuChoice = " << menuChoice << "\n\n";
/*int result = */displayMenu(menuChoice);
std::cout << "\n After the function call the value of menuChoice = " << menuChoice << "\n\n";
// <--- These lines I do not understand what they are for yet, but it is becomming more clear. Not important yet.
//cout << "You entered " << getData();
//cout << "You selected" << result;
return 0; // <--- Not required, but makes a good break point.
}
|
This is not meant to replace what you have permenantly, but to help understand the pass by reference part.
When "main" calls "displayMenu" you do have a problem:
B4 the function call the value of menuChoice = 0
1) Is the number odd or even?
2) Is the number positive or negative?
3) What is the square root of the number?
4) How many digits are in the number?
5) Digit Extraction.
6) Display Addition Table.
7) Display Multiplication Table.
8) Exit
Enter a menu choice: 10
After the function call the value of menuChoice = 10
|
As you can see the function does allow the entry of an invalid choice. You can either take care of this in the "displayMenu" function or later in "processMenuChoice". If you take of an invalid menu choice later this will cause some big changes in the "processMenuChoice" function. Easier to deal with everything in the "displayMenu" function.
Here is an example. I know there are better ways to write this function, but I am thinking that this would be easier to understand at first. Save the improvements for later. For now understand how it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
void displayMenu(int & menuChoice)
{
do
{
cout <<
"\n1) Is the number odd or even?\n"
"2) Is the number positive or negative?\n"
"3) What is the square root of the number?\n"
"4) How many digits are in the number?\n"
"5) Digit Extraction.\n"
"6) Display Addition Table.\n"
"7) Display Multiplication Table.\n"
"8) Exit\n"
" Enter a menu choice: ";
cin >> menuChoice;
if (!std::cin || (menuChoice < 1 || menuChoice > 8))
{
if (!std::cin)
{
std::cerr<<"\n Invalid Input! Must be a number.\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
else if (menuChoice < 1 || menuChoice > 8)
{
std::cerr << "\n Invalid menu Choice! Try again.\n\n";
}
}
} while (menuChoice < 1 || menuChoice > 8);
}
|
The do/while loop keeps you in the function until you have entered a valid menu choice.
In your cout statement the insertion operator (<<) is not necessary. Only the first 1 is. Since everything is a quoted string I believe that "cout" will see this as 1 big string before it sends it to the display.
The first part of the first if statement will become true if anything other than a number is entered. Otherwise "cin" will be in a failed state and unusable anywhere in the program until it is fixed. Also there a chance that "menuChoice" may be set to zero if "cin" fails and that is not what you want.
When everything is correct the function will end and return to "main". The variable "menuChoice" being passed by reference will change the value of "menuChoice" in "main".
After this much is working you can move on to the "processMenuChoice" function. Doing whatever is need to send the correct parameters to the function.
An after thought. Your function
void displayMenu(int & val)
it is acceptable to change the name of the variable in the function, but I find it easier to work with if you do not change the name. Here "menuChoice" gives the function a better understanding of what you are using than "val" does. Also whether the variable is defined as a function parameter or after the opening { it is a local variable to the function. Only the pass by reference makes any difference, but it does not matter what the variable name is in the function.
Now that I have made changes to the the 1st part "displayMenu" I will add and work on the "processMenuChoice" function.
Andy