Hello john26999,
The main problem is in the "Addition" function.
I have not worked on fixing it yet, but did make comments that should help:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
string Addition(int sum, int numVal) // <--- There is no real need for the parameters.
// Or you could pass "sum" by reference to use back in "WelcomeScreen".
// Or return "sum" back to "WelcomeScreen".
{
char q{ 'q' }; // Local Variable. Originally uninitialized and contained garbage.
sum = 0; // Local variable that is initialized. / Local variable that is set to zero.
//int numVal{}; // <--- Should be this. Not defined as a parameter.
cout << "Enter a positive or negative integer and then press enter to continue "
<< "adding new integers or press 'q' for solution: \n";
cin >> numVal;
while (!(numVal == q)) // <--- Compares an "int" to a "char". Does not work the way that you think.
{
cin >> numVal;
sum = sum + numVal;
}
// Function call to display the results of the integers
DisplayResult(sum); // <--- Should be done in "WelcomeScreen" or better in "main".
return "?"; // <--- You promise to return something, but you did not.
}
|
The next problem is line 12. This is formatted input and "cin" is expecting to put a number into the variable "numVal". When you enter "q" this causes "cin" to fail and is unusable after that point, so every "cin" you come to is passed over because it no longer works.
One possible solution is to make "numVal" or something else a "std::string" for the input and along with a try/catch change the string into an "int" with "stoi()" from the string class. If this should fail the catch block will keep the program running.
In the function definition you are passing two variables by value, i.e., only a copy. These are better defined in the function as it is written.
As the comments suggest I would loose the second parameter and consider passing "sum" by reference to be used back in "WelcomeScreen" where it was first defined.
This way you could move the "DisplayResult" function back to "WelcomeScreen" in the switch where it belongs.
Another my VS2017 compiler had was the most of the variables are defined uninitialized and then used before they have received any usable value.
You have included the header file "math.h". First you should use "cmath" and second I do not see anything like "pow()" or "sqrt()" or anything else that comes from "cmath". "cmath" is not required for general (+, -, *, / or %) arithmetic .
You could also benefit from including the "cctype" header file and using "std::tolower()" or "std::toupper()" or when you enter a letter like "q" you should check for either case.
Hope that helps,
Andy