a problem with a calculator code

hi
i have made a calculator code with if statement.
but i dont know why it doesnt work correctly.
it attentions on the first if task without including the condition.
so it only plus the first num to second num!
wether you put +,-,/,* it only add them to each other!


this is the code:

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
  #include <iostream>
using namespace std;
void main(){
	float a;
	char b;
	float c;
	float d;
	cout << "enter the amaliat to collect" << endl << "enter the firs number" << endl;
	cin >> a;
	cout << "enter the operation that you want including +,-,*,/" << endl;
	cin >>b;
	cout << "enter the second number " << endl;
	cin >>c;
	if (b = '+'){
		cout << a + c << endl;
	}
	else if (b = '-'){
		cout << a - c << endl;
	}
	else if (b = '/'){
		cout << a / c << endl;
	}
	else if (b = '*'){
		cout << a*c << endl;
	}
	else if (b != '/', '+', '-', '*'){
		cout << "the operation that you entered in incorrect" << endl;
	}
	do{
		cout << "enter the amaliat to collect" << endl << "enter the firs number" << endl;
		cin >> a;
		cout << "enter the operation that you want including +,-,*,/" << endl;
		cin >> b;
		cout << "enter the second number " << endl;
		cin >> c;
		if (b = '+'){
			cout << a + c << endl;
		}
		else if (b = '-'){
			cout << a - c << endl;
		}
		else if (b = '/'){
			cout << a / c << endl;
		}
		else if (b = '*'){
			cout << a*c << endl;
		}
		else if (b != '/', '+', '-', '*'){
			cout << "the operation that you entered in incorrect" << endl;
		}
	} while (b > -9999999999999999);
	
}





and also how can i repeat the code without do and while?for example repeat the code until the answare is a float?
oh yes!
i fixed it!
i only needed an extra=.
but what about the second question?
Last edited on
if (b = '+')
this is an assignment. It says, assign b to the plus character, and then evaluate to TRUE (because the + symbol is not zero, it is true, if this had been '\0' it would still be wrong and FALSE).

you want

if (b == '+')
which says "if b is the plus symbol, true, otherwise false"


the while condition may also bug out. If it overflows the wrong way, which I didnt investigate, it could actually fail in a strange way. Just make it -999. B is a char, it can't be more than +- 128, so -999 is more than sufficient. However -999 is also a bit off, just do while(true) to loop forever. There is no good reason to put a complex condition that can't fail in.
Last edited on
thanks just now i undrestood!
a little late!
you got the repeat to work?

a simplistic way to do that is
while (b!= '/' ||(b=='/'&& x%y != 0))

you can't repeat without a loop of some sort or a goto (gotos are generally bad programming).
you can interchange all loops (with some effort at times) but do-while seems right here.
Last edited on
thanks for youre time jonnin.
it was helpful.
Topic archived. No new replies allowed.