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.

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
33
34
35
36
37
38
39
40
41
#include <iostream>
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:

1
2
3
4
5
6
if (!(cin >> val1))
{
    // Do error stuff
    // ...
    cin.clear();  // clear error state.
}


Edit yeah, I was right. See here for more info:

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.
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();
	}
	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.
1
2
3
4
5
6
7
8
9
10
11
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:
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.

Remember again that whitespace is ignored.
yup..... I can hit 3 + 4 (enter)
then it blinks
I enter 5 (enter) and then I get
The sum of 4 and 5 is 9
Strange huh?
Can you post your entire code here?
Topic archived. No new replies allowed.