calculator code problem!

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
    #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?
@garyshorto

On lines 14, 17, 20, 23, 36, 39 42 and 45 you are using '=', which is for assigning a value to a variable, instead of checking for equality, which is '=='. Then, on lines 26 and 48, you are using incorrect statements. You must precede each check, with the variable you are checking. As in else if (b != '/' && b != '+' && b != '-' && b != '*') The '&&' symbols means AND. So, this statement reads "If b does not equal '/' and b does not equal '+' and b does not equal '-' and b does not equal '*'', THEN do the following.

We can get to the rest of your request after getting what you have, working properly.
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
#include <iostream>

int main() // note: int main()
{
    double first, second ; // favour double over float
    char oper ;

    // this loop continues as long as the user provides a valid input
    // ie. as long as the attempted input is successful
    while( std::cout << "\nenter <first number> [+-*/] <second number>\n"
                     << "eg. 23.45 * 3.8  (enter 'q' to quit):\n? " &&
           std::cin >> first >> oper >> second )
    {
        // the alternative is to use a switch-case construct
        if( oper == '+' ) std::cout << first+second << '\n' ;
        else if( oper == '-' ) std::cout << first-second << '\n' ;
        else if( oper == '*' ) std::cout << first*second << '\n' ;
        else if( oper == '/' ) std::cout << first/second << '\n' ;
        else std::cout << "invalid operation\n" ; // not one of +=*/

        // for division first / second,
        // see what would happen if second is zero?
        // see what would happen if both first and second are zero?
    }
}
Topic archived. No new replies allowed.