the logic of equality operators

Hello, i I'm a beginner in the arts of c++ and stumbled upon a little logical question while doing a practice program I designed myself about equations.

Don't know how to post the code as IDE-like code so if someone can enlighten me,
please do
. (This have been corrected.)

At the first if equation if (a == b, a == c). I first wrote if (a == b, b == c)
The problem was that if the first entry was 100, and the 2 last was, say 50
it reported all 3 numbers to match.


I found the problem but don't know why this is correct and the other not.
The program is as follows.

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

int main ()

{
	
	int a, b, c;	// Declares 3 integer variables.
	
	/* The program starting text */
	cout << "Enter 3 numbers and the program will check if they match." << endl;
	
	
	cout << "Enter a number: ";   // Promt for the first input.
	cin >> a;  
	
	
	cout << "Now enter another: "; // Second promt.
	cin >> b; 	
	
	
	cout << "Now for the final number: "; // Final promt.
	cin >> c;  
	
		
		if (a == b, a == c) // This one checks if all 3 input matches.
		
			cout << "All 3 numbers match."; 
		
		else if (a==b)	
		
			cout << "The first and second numbers match.";
		
		else if (a==c)
	
			cout << "The first and last mumbers match.";
		
		else if (b==c)
	
			cout << "The second and last numbers match.";
		
		else
	
			cout << "None of the numbers you entered match each other."; 
	
	return 0;
}
Last edited on
Replace the comma in the if statement with &&. && is the AND logic operator, and checks first whether the left is true, and then whether the right is true. If they are both true, than it returns true.
Ah, thanks for quick answer. I will replace it, you mean like this? (a == b && b == c)?
That will work perfectly. Also, next time you post code, be sure to use the ['code][/code] tags, just so that it is easier to read (being sure to get rid of that apostrophe in the first half of the tag).
Last edited on
Thanks, i corrected the code-tags now =)

Also, the program runs fine now even if I use if (a == b && b==c ) now =D
Last edited on
Topic archived. No new replies allowed.