Help!

Okay so I'm new. I need to make a simple calculator, that asks the user to choose and operator, then do the math. So I thought this code would work, but it won't build because == doesn't work...? I don't understand the comments on the bottom that well. Your help is really appreciated.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
using namespace std;


int main()
{
	int number1, number2;
	char op;

	cout << "Which arithmetic operator "
		<< " would you like to use (+,-,*,/,%) ?"
		<< endl;
	cin >> op;

	{
		if (!op == '/' || op == '+' || op == '-' || op == '*' || op == '%')
			cout << "This is an invalid operator";
		else if (op == '+')
		{
			cout << "\nPlease enter the two operands "
				<< " for addition";
			cin >> number1, number2;
			cout << "\nThe sum of " << number1 << " and "
				<< number2 << " is " << number1 + number2;

		}

		else if (op == '-')
		{
			cout << "\nPlease enter the minuend" << endl;
			cin >> number1;
			cout << "\nPlease enter the subtrahend" << endl;
			cin >> number2;
			cout << "\nThe difference of " << number1 << " and "
				<< number2 << " is " << number1 - number2;

		}

		else if (op == '*')
		{
			cout << "\nPlease enter the first factor" << endl;
			cin >> number1;
			cout << "\nPlease enter the second factor" << endl;
			cin >> number2;
			cout << "\nThe product of " << number1 << " and "
				<< number2 << " is " << number1 * number2;
		}

		else if (op == '/')
		{
			cout << "\nPlease enter the dividend" << endl;
			cin >> number1;
			cout << "\nPlease enter the divisor" << endl;
			cin >> number2;
			{
				if (number2 == 0)
					cout << "\nThis is an invalid divisor";
				else
					cout << "\nThe quotient of " << number1
					<< " and " << number2 << " is "
					<< number1 / number2;
			}
		}

		else if (op == '%')
		{
			cout << "\nPlease enter the dividend" << endl;
			cin >> number1;
			cout << "\nPlease enter the divisor" << endl;
			cin >> number2;
			{
				if (number2 == 0)
					cout << "\nThis is an invalid divisor";
				else
					cout << number1 << " modulus " << number2
					<< " is " << number1 % number2;
			}
		}
	}

}
	system("pause");
	return 0;





sorry for the stupid brackets. I hate the brackets.
You have missed two brackers in your condition:

 
if (!(op == '/' || op == '+' || op == '-' || op == '*' || op == '%'))
Your code didn't compile because of lines 82 and 83, they were not in the scope of the main() function.

The if statement that checks for a valid operator on line 16 of your code was wrong, there really is no need for it, it's better to have an else statement at the bottom.

When comparing one variable to a set of constants prefer using a switch statement in stead of if else if else statements.

Lines 15 and 79 are obsolete, you do not need a nested scope.

Line 22 of your code was incorrect.

Never use system(), here's why http://www.cplusplus.com/articles/j3wTURfi/

There is a lot of repetition of code, cin >> number1; and cin >> number1;, find a way to avoid that.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;


int main()
{
	int number1, number2;
	char op;

	cout << "Which arithmetic operator "
		<< " would you like to use (+,-,*,/,%) ?"
		<< endl;
	cin >> op;

	{
		if (op == '+')
		{
			cout << "\nPlease enter the two operands "
				<< " for addition";
			cin >> number1;
			cin >> number2;
			cout << "\nThe sum of " << number1 << " and "
				<< number2 << " is " << number1 + number2;

		}

		else if (op == '-')
		{
			cout << "\nPlease enter the minuend" << endl;
			cin >> number1;
			cout << "\nPlease enter the subtrahend" << endl;
			cin >> number2;
			cout << "\nThe difference of " << number1 << " and "
				<< number2 << " is " << number1 - number2;

		}

		else if (op == '*')
		{
			cout << "\nPlease enter the first factor" << endl;
			cin >> number1;
			cout << "\nPlease enter the second factor" << endl;
			cin >> number2;
			cout << "\nThe product of " << number1 << " and "
				<< number2 << " is " << number1 * number2;
		}

		else if (op == '/')
		{
			cout << "\nPlease enter the dividend" << endl;
			cin >> number1;
			cout << "\nPlease enter the divisor" << endl;
			cin >> number2;
			{
				if (number2 == 0)
					cout << "\nThis is an invalid divisor";
				else
					cout << "\nThe quotient of " << number1
					<< " and " << number2 << " is "
					<< number1 / number2;
			}
		}

		else if (op == '%')
		{
			cout << "\nPlease enter the dividend" << endl;
			cin >> number1;
			cout << "\nPlease enter the divisor" << endl;
			cin >> number2;
			{
				if (number2 == 0)
					cout << "\nThis is an invalid divisor";
				else
					cout << number1 << " modulus " << number2
					<< " is " << number1 % number2;
			}
		}
		
		else
		{
			cout << "Invalid operator" << endl;
		}
	}
	
	return 0;
}
Last edited on
but it won't build

What do you mean "it won't build"?

Please post the exact compiler error messages.

I don't understand the comments on the bottom

What comments? I don't see a single one.

Line 16: You need to check out order of operations. See the table at the bottom of this page: http://www.cplusplus.com/doc/tutorial/operators/

What do you think the ! applies to? Hint: The compiler thinks is applies only to the first instance of op. Not the entire boolean expression.

Line 81: This } is out of place. It belongs after line 82.

Line 15,79: This pair of {} is unnecessary.

Last edited on
okay update-- got it to work. The code compiles successfully and debugs. But when I was testing it-
it showed the first cout, asking the op. I put in +. It asked for the two numbers. I typed in 10
12
and then the app just exited itself.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;

int main()
{
	int number1, number2;
	char op;

	cout << "Which arithmetic operator would "
		 << " you like to use?" << endl;
	cin >> op;

	{
		if (op == '+')
		{
			cout << "\nPlease enter the two operands "
				<< " for addition " << endl;
			cin >> number1;
			cin >> number2;
			cout << "\nThe sum of " << number1 << " and "
				<< number2 << " is " << number1 + number2;
		}


		else if (op == '-')
		{
			cout << "\nPlease enter the minuend" << endl;
			cin >> number1;
			cout << "\nPlease enter the subtrahend" << endl;
			cin >> number2;
			cout << "\nThe difference of " << number1 << " and "
				<< number2 << " is " << number1 - number2;
		}

		else if (op == '*')
		{
			cout << "\nPlease enter the first factor" << endl;
			cin >> number1;
			cout << "\nPlease enter the second factor" << endl;
			cin >> number2;
			cout << "\nThe product of " << number1 << " and "
				<< number2 << " is " << number1 * number2;
		}

		else if (op == '/')
		{
			cout << "\nPlease enter the dividend" << endl;
			cin >> number1;
			cout << "\nPlease enter the divisor" << endl;
			cin >> number2;
			{
				if (number2 == 0)
					cout << "\nThat is an invalid divisor";
				else
					cout << "\nThe quotient of " << number1
					<< " and " << number2 << " is "
					<< number1 / number2;
			}

		}

		else if (op == '%')
		{
			cout << "\nPlease enter the dividend" << endl;
			cin >> number1;
			cout << "\nPlease enter the divisor" << endl;
			cin >> number2;
			{
				if (number2 == 0)
					cout << "\nThat is an invalid divisor";
				else
					cout << endl << number1 << " modulus "
					<< number2 << " is " << number1 % number2;
			}
		}

		else
		{
			cout << "\nInvalid operator. Please choose (+,-,*,/,%)"
				<< endl;
		}
	}
	
	return 0;

}


also- about the comments. Sorry I wasn't clear earlier-- they are the ones at the bottom of microsoft visual studios. But I worked that bit out now. Fixed one thing and messed up the other.

But after the app exits the new comments say
Cannot find or open the PDB file.
The program '[6656] asgmt2.1.exe' has exited with code 0 (0x0).
Last edited on
and then the app just exited itself.

After your code executes the cout at line 20, it executes the return at line 84 (exits) which causes the CMD window to close.
http://www.cplusplus.com/forum/beginner/1988/

Cannot find or open the PDB file.

That's just a normal message from Visual Studio indicating it can't open the debugging infomation file for one of the Windows DLLs. .pdb files don't come with Visual Studio. You need the Windows SDK if you want to debug inside Windows DLLs.



Topic archived. No new replies allowed.