Calculator

I built a calcuator and am geting an error at the "==" in the if statments. Can some one help me please?


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
#include <iostream>
#include "conio.h"
using namespace std;

void main()
{

	int Opperation, Num1, Num2, Answer, Calculate = true;
	do{
	cout << "Would you like to add, subtract, divide or multiply?" << endl;
	cin >> Opperation;
	cout << "Please enter two numbers" << endl;
	cin >> Num1;
	cout << endl;
	cin >> Num2;
	cout << endl;

	if (Opperation == "add")
		{
			Answer = Num1 + Num2;
			cout << Num1 << "+" << Num2 << "=" << Answer << endl;
		}
	else if (Opperation == "subtract")
		{
			Answer = Num1 - Num2;
			cout << Num1 << "-" << Num2 << "=" << Answer << endl;
		}
	else if (Opperation == "divide")
	
		{
			Answer = Num1 / Num2;
			cout << Num1 << "/" << Num2 << "=" << Answer << endl;
		}
	else if (Opperation == "multiply")
		{
			Answer = Num1 * Num2;
			cout << Num1 << "*" << Num2 << "=" << Answer << endl;
		}

			cout << "Would you like do to anouther calculation? For yes say true for no say false" << endl;
			cin >> Calculate;
	}while(Calculate == true);
			

}


also in line 41 i "cin" true... will that work or will i have to use a special integer type?

thanks for the help in advance :)
Last edited on
You can't compare an integer to a string. Make the variable "Opperation" into a string. Also double check your spelling of the word Opperation.
On line 8, does the 'Calculate' variable need to be stated on a separate line as:
bool Calculate=true;
Doesn't sound like you had a problem with it, but I don't understand why not :S

I'm a beginner, so don't take my C++ as 100% correct.
@Krakow10
Technically, no, it doesn't need to be. You can assign true or false to integer values (after all, that's essentially what they are). Generally, it'll be interpreted that zero is false and anything non-zero is true. I'd wager that if you printed Calculate to the screen, it would show as one.

However, just because it's valid C++ doesn't mean it's good C++. It's a pretty bad way of coding it. It should, as your example suggested, really be expressed as a boolean. However, given that the trouble the OP is having I'm not sure they've quite grasped the correct use of data types yet.
Last edited on
Opperation and Calculate should be strings.
 
std::string Opperation, Calculate;


and }while(Calculate == true);
should be
}while(Calculate == "true");
Topic archived. No new replies allowed.