what is the difference between == and =

what is the difference between == and =?
i tried this out with a primitive calculator that i created

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
#include <iostream>
using namespace std;

int main ()
{
	//first number
	 int a;
	// operation	
	 int b;
	//second number		
	 int c;

		cout << "Select your first number ";
		cin >> a;

			cout << "Select the operation ";
			cout << "1= add, 2= sub, 3= multiply, 4= divide";
			cin >> b;

				cout << "Select the second number ";
				cin >> c;

				if (b= 1) cout << " Your Result is " << a + c;
				if (b= 2) cout << " Your Result is " << a - c;
				if (b= 3) cout << " Your Result is " << a * c;
				if (b= 4) cout << " Your Result is " << a / c;

				cin.ignore(LONG_MAX,'\n');
				cin.get ();
					return 0;
    }


but when i changed

1
2
3
4
if (b= 1) cout << " Your Result is " << a + c;
				if (b= 2) cout << " Your Result is " << a - c;
				if (b= 3) cout << " Your Result is " << a * c;
				if (b= 4) cout << " Your Result is " << a / c;


to this

1
2
3
4
if (b= 1) cout << " Your Result is " << a + c;
				if (b== 2) cout << " Your Result is " << a - c;
				if (b== 3) cout << " Your Result is " << a * c;
				if (b== 4) cout << " Your Result is " << a / c;


it worked
Don't double post.
== is a logical operator. It returns true if its two operands have the same value and false otherwise.
= is the assignment operator. It makes the left operand equal the right. I don't know if it has a return.
When you put = in an if statement expecting an ==, it performs its usual operation. The single equals sign DOES NOT check if the operands are equal. It assigns one to the other. You need the logical == operator to perform the equality check.
This is an exceptionally simple question and you should check this website's tutorial before you post something like this. Your explanation, in longer terms, can be found here: http://www.cplusplus.com/doc/tutorial/operators/
Last edited on
The proper term for operators is not "returns". It's "evaluates to".

The assignment operator evaluates to its right hand operand (or the left hand operand's new value, which is the same).
Topic archived. No new replies allowed.