### Char value == 0? Non-numerical values interpreted as zero

I am testing my Calculator program's error message by inputting random letters and punctuation, hoping to see the error message "Error, please make your selection again". However, every non-integer key that I press is read as zero and is diverted to my final if statement:
 ``1234`` `````` if (selection == 0) { exit(1); }``````

That is, the program exits with a value of 1. Why does this happen, and how can I fix my code. Thank you.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154`` ``````#include #include #include using namespace std; int AddNum(int, int); int SubNum(int, int); int MultNum(int, int); double DiviNum(int,int); int ModNum(int, int); int PowNum(int, int); double ResultSin(int); double ResultCos(int); const float PI = 3.14159265; int main () { int selection; int num1; int num2; for(;;){ cout << "********************CALCULATOR*********************" << endl; cout << "1. Addition" << endl; cout << "2. Subtraction" << endl; cout << "3. Multiplication" << endl; cout << "4. Division" << endl; cout << "5. Modular Division" << endl; cout << "6. Raise x to the power of y" << endl; cout << "7. Sine(x)" << endl; cout << "8. Cosine(x)" << endl; cout << " " << endl; cout << "0 - Quit" << endl; cout << "***************************************************" << endl; cout << "Enter a selection please" << endl; cin >> selection; if (selection == 1) { cout << "Enter first number:" << endl; cin >> num1; cout << "Enter next number:" << endl; cin >> num2; cout << "The sum of " << num1 << " and " << num2 << " is " << AddNum(num1, num2) << endl; } else if (selection == 2) { cout << "Enter first number:" << endl; cin >> num1; cout << "Enter next number:" << endl; cin >> num2; cout << "The difference of " << num1 << " and " << num2 << " is " << SubNum(num1, num2) << endl; } else if (selection == 3) { cout << "Enter first number:" << endl; cin >> num1; cout << "Enter next number:" << endl; cin >> num2; cout << "The product of " << num1 << " and " << num2 << " is " << MultNum(num1, num2) << endl; } else if (selection == 4) { cout << "Enter first number:" << endl; cin >> num1; cout << "Enter next number:" << endl; cin >> num2; cout << "The quotient of " << num1 << " and " << num2 << " is " << DiviNum(num1, num2) << endl; } else if (selection == 5) { cout << "Enter first number:" << endl; cin >> num1; cout << "Enter next number:" << endl; cin >> num2; cout << "The remainder of " << num1 << " / " << num2 << " is " << ModNum(num1, num2) << endl; } else if (selection == 6) { cout << "Enter first number:" << endl; cin >> num1; cout << "Enter next number:" << endl; cin >> num2; cout << num1 << " to the power of " << num2 << " is " << PowNum(num1, num2) << endl; } else if (selection == 7) { cout << "Enter number:" << endl; cin >> num1; cout << "Sine(" << num1 << ")" << " is " << ResultSin(num1) << endl; } else if (selection == 8) { cout << "Enter number:" << endl; cin >> num1; cout << "Cos(" << num1 << ")" << " is " << ResultCos(num1) << endl; } else if (selection == 0) { break; } else { cerr << "Error, please make your selection again" << endl; } cout << "Press any key to continue" << endl; system("read"); }; if (selection == 0) { exit(1); } return 0; } int AddNum(int num1, int num2) { return num1+num2; } int SubNum(int num1, int num2) { return num1-num2; } int MultNum(int num1, int num2) { return num1*num2; } double DiviNum(int num1, int num2) { return num1/num2; } int ModNum(int num1, int num2) { return fmod (num1, num2); } int PowNum(int num1, int num2) { return pow(num1,num2); } double ResultSin(int num1) { return sin(num1*PI/180); } double ResultCos(int num1) { return cos(num1*PI/180); } ``````
Last edited on
If cin >> operator fails, that means that selection will still have its previous value, which in this case is a junk value (which I suppose happens to be 0 in this case, but in reality could be undefined).

You should instead first check to see if the cin >> operation was successful.
 ``123456789`` ``````if (cin >> selection) { // Success, now you can properly check the value of selection } else { cerr << "Error, please make your selection again" << endl; cin.clear(); }``````
Last edited on
That would work, however if I use "cin >> selection" twice it produces an error where I have to enter the input value twice into the prompt before it will register. Is there a way to do this without using "cin >> selection" twice?
I figured out a way to make that work however it is still throwing out related errors. I'm going to mark this as complete though because my question was essentially resolved.