If/else help

Hey folks I just started learning C++ a week ago and have been stuck on a project for the past 2 days now. I am building a limited purpose calculator which finds the value of one of five operations. Visual studio doesn't underline any errors in my program but every time I try to run it I get an error message. I believe it has something to do with the if/else but Im not sure. This isnt a project from my book so it has been no help. If anyone has the time and patience I would greatly appreciate you taking a look at my program and telling me what I am doing wrong. 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
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

int main()
{
	int a;
	int b;
	
	int sum = a + b;
	int difference = a - b;
	int product = a * b;
	int quotient = a / b;
	int square = a * a;

	cout << "Enter a number " << endl;
	cin >> a;

	cout << "Enter another number " << endl;
	cin >> b;

	cout << "Enter an operation." << endl;
	cin >> sum, difference, product, quotient, square;
	
	if (sum)
	{
		cout << "The sum is " << endl;
		cout << sum << endl;
	}
	else if (difference)
	{
		cout << "The difference is " << endl;
		cout << difference << endl;
	}
	else if (product)
	{
		cout << "The product is " << endl;
		cout << product << endl;
	}
	else if (quotient)
	{
		cout << "The quotient is " << endl;
		cout << quotient << endl;
	}
	else 
	{	
		cout << "The square is " << endl;
		cout << square << endl;
	}
	
system("pause");
	return 0;
}
Last edited on
What are the values of a and b when lines 9-13 are executed?
Hint: You can't do a calculation on two numbers before you've assigned values to them.

What is line 22 supposed to be doing? You're asking the user to enter 5 integers.
I ran your program and got a floating point exception. The floating point exception is due to
 
int quotient = a / b;

Since you don't initialize a or b to anything, this is initially 0 / 0 which is NaN (not a number). If you simply cast a or b to a floating point number in this equation:
 
int quotient = static_cast<double>(a) / b;

or
 
int quotient = a / static_cast<double>(b);

The floating point exception goes away. This is because if either a or b is a floating point, when the operation is done, it promotes the other to a floating point.

If you simply make b initialized to something other than 0, the floating point exception also goes away. If you divide a number by zero it is either NaN of +/- infinity, which is not an integer.

Keeping a and b both at zero and simply changing quotient to a double won't fix it (i.e. double quotient = a/b; because when a / b is initially calculated both a and b are int's so the solution is of type int and only after it is calculated is the solution (assigned to the variable quotient) converted to a floating point number. Look through type conversion in a C++ book, operations are best done with float or double because 5/2 = 2 with ints.


Also, you are correct that your if/else has problems and so does you cin and you also need to recompute sum/difference/etc. after you read a and b in. As for your if/else, if(sum) (and all the other else if's) convert the variable in the parentheses to a boolean (meaning it can only have the value of 0 or 1). For boolean's 0 = false, 1 = true. If another type is converted to a boolean, 0 = false and anything other than zero evaluates to true.

Keeping things as simple as possible, what you actually want to do is make a string called operation. Read in operation, and use that for your if/else:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
...
double a = 0., b = 1.;
string operation = "+";
cout << "Enter a number" << endl;
cin >> a;
cout << " enter another number" << endl;
cin >> b;
cout << "enter an operation (+, -, *, /, sq)" << endl;
cin >> operation;

if(operation == "+")
  cout << "The sum is " << (a+b) << endl;
else if(operation == "-")
  cout << "The difference is " << (a-b) << endl;
...
else if(operation == "sq")
  cout << "The square is " << (a*a) << endl;
else
  cout << "Not a valid operation. Valid operations are +, -, *, /, sq" << endl;


By the way, system("pause") works on Windows but this doesn't work on most UNIX systems. You may not care about cross-platform compatibility right now but it's generally bad practice to do so.

Yes! thank you based anon! It works kind of!

the values of a and b are what ever you input so I moved them below 15-19 and I fixed line 22 as well. Now it runs but it only calculates the sum of everything even if I enter difference or product. Hopefully I can tinker around to fix that but at least it actually runs now!

thank you thank you thank you!
Actually, you can do calculations on two numbers before you've assigned values to them because they are assigned to space a memory regardless of whether you set what value to be placed in that memory location. The newer C++ standards (starting with C++11, I believe, maybe C++0x) guarantee that uninitialized variables are set to their equivalent of zero (0 for int/unsigned/float/double/etc.) or empty for char/string. Older standards will usually have a random value from what was previously stored in its place in memory previously.
Last edited on
jmadsen, I much appreciate you looking over my program. Embarrassingly I forgot that zero is an integer too...I noticed that when anon helped me fix it a minute ago, running the program performs every operation except for division so I am trying to fix that right now. I am certainly going to have to tinker with this program quite a bit more to make it do what I want.

And thanks for posting a snippet of how you would code it, I don't want to copy it but it really simplifies my program quite elegantly so I am definitely going to study it and maybe borrow some tricks. Also thanks for the heads up on Unix, I'm obviously a windows user although I plan to one day install a linux distro (maybe after I can create programs on my own!). Thanks you so much for the guidance Ill post a status update after I sort some things out.
Topic archived. No new replies allowed.