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.
That's not working.... I put that on top of the if statement.... and made the top one an else if statement below it.
1 2 3 4 5 6 7 8 9 10
if (!(cin >> val1))
{
cout << "There was an error processing your request...\n"
<< "The program will now terminate.\n";
cin.clear();
}
elseif ((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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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.