### Easy Calculator with doubles

I have a problem... I made an easy calculator but having trouble.
The problem is that it works... but my else if on the bottom is not working.
I want it to give a error if the user inputs anything other than a double or integers or the +, -, /, *, % operators.

Every time I use a letter to mess it up it gives me a ridiculous number because I believe its getting the number of each letter or character... and !isdigit only gets whole numbers. Here is the code - any helpers out there?
I understand I could do it in a Case format but I want to do in in an If else If way. Thanks.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include using namespace std; int main() { double val1; double val2; char inputOperator; cout << "Please enter two values and an operator of +, -, /, * or %\n" << "Example:\n" << "35.6 + 24.1 (Press Enter)\n" << "The sum of 35.6 and 24.1 is 59.7.\n\n"; cin >> val1 >> inputOperator >> val2; if ((val1 <= 0) || (val1 >= 0) && (val2 <= 0) || (val2 >= 0) && inputOperator == '+') { cout << "The sum of " << val1 << " and " << val2 << " is " << val1 + val2 << ".\n"; } else if ((val1 <= 0) || (val1 >= 0) && (val2 <= 0) || (val2 >= 0) && inputOperator == '-') { cout << "The difference of " << val1 << " and " << val2 << " is " << val1 - val2 << ".\n"; } else if ((val1 <= 0) || (val1 >= 0) && (val2 <= 0) || (val2 >= 0) && inputOperator == '/') { cout << "The ratio of " << val1 << " and " << val2 << " is " << val1 / val2 << ".\n"; } else if ((val1 <= 0) || (val1 >= 0) && (val2 <= 0) || (val2 >= 0) && inputOperator == '*') { cout << "The product of " << val1 << " and " << val2 << " is " << val1 * val2 << ".\n"; } else if ((val1 <= 0) || (val1 >= 0) && (val2 <= 0) || (val2 >= 0) && (inputOperator == '%')) { cout << "The modulus of " << val1 << " and " << val2 << " is " << fmod(val1, val2) << ".\n"; } else if (!(val1) || !(val2) || (inputOperator != '+') || (inputOperator != '-') || (inputOperator != '/') || (inputOperator != '*') || (inputOperator != '%')) { cout << "There was an error processing your request...\n" << "The program will now terminate.\n"; } return 0; }``````
IIRC, you do a check like this:

 ``123456`` ``````if (!(cin >> val1)) { // Do error stuff // ... cin.clear(); // clear error state. }``````

http://augustcouncil.com/~tgibson/tutorial/iotips.html
Last edited on
That's not working.... I put that on top of the if statement.... and made the top one an else if statement below it.
 ``12345678910`` `````` if (!(cin >> val1)) { cout << "There was an error processing your request...\n" << "The program will now terminate.\n"; cin.clear(); } else if ((val1 <= 0) || (val1 >= 0) && (val2 <= 0) || (val2 >= 0) && inputOperator == '+') { cout << "The sum of " << val1 << " and " << val2 << " is " << val1 + val2 << ".\n"; }``````

Once I hit enter ... it doesn't do anything... until I hit another number then enter.... then it grabs the 2nd number and the operator and the 3rd number and does the equation.... the 1st number is ignored... weird error.
Last edited on
Ok if you say you was right... why am I still getting that error ..

I added the <iomanip> and <fstream> header files at top next to my
<iostream> one

or perhaps im am confused by the information in the link.... could you clarify please :)
It works without the >> val1 so this works.
 ``1234567891011`` ``````if (!(cin)) { cout << "There was an error processing your request...\n" << "The program will now terminate.\n"; cin.clear(); } else if ((val1 <= 0) || (val1 >= 0) && (val2 <= 0) || (val2 >= 0) && inputOperator == '+') { cout << "The sum of " << val1 << " and " << val2 << " is " << val1 + val2 << ".\n"; } ``````
Last edited on
It doesn't work? Strange. So you type in garbage and hit enter and it doesn't 'do anything'? Like the cursor moves to the next line and just keeps on blinking? If you don't type anything but whitespace, it will not do anything as it considers whitespace as that nothing. Is that what you are referring to?

The return value of `cin >> val1` should be a reference to `cin`. That's how the `istream::operator>>()` works.

Hmmmm.

BTW, if you are just using cin by itself, like: `if(!(cin))` then you can remove the parenthesis like so: `if(!cin)`.

Also, if you are just terminating, you could do:
 ``123456789101112131415`` ``````if (cin >> val1 >> op >> val2) { if ((val1 <= 0) || (val1 >= 0) && (val2 <= 0) || (val2 >= 0) && inputOperator == '+') { ... } else ... { } } else { cout << "There was an error processing your request...\n" << "The program will now terminate.\n"; cin.clear(); }``````

The first error it gets, the rest of the `istream::operator>>()` calls will abort.

Remember again that whitespace is ignored.
yup..... I can hit 3 + 4 (enter)