what is the difference between == and =

Jan 13, 2010 at 2:30am
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
Jan 13, 2010 at 6:01am
If as you say - you changed from 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;


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;


You are still wrong!
Last edited on Jan 13, 2010 at 6:01am
Jan 13, 2010 at 1:07pm
One is the assignment operator, one is the equality operator.

= is the assignment operator. b = 1 will set the variable b equal to the value 1.

== is the equality operator. it returns true if the left side is equal to the right side, and returns false if they are not equal

With only rare exceptions, you should not be using the assignment operator inside an if clause, but that is an extremely common beginner mistake.
Jan 13, 2010 at 10:13pm
tyvm i get it now
Topic archived. No new replies allowed.