Testing the equality operator

Hi,
I am learning C++ for the first time and I am having a small problem that is keeping me from moving ahead in the book. In Python whenever I am testing out the == operator it works as planned.
In c++, for example:

if(variable1 == variable2)
cout<<"Both numbers are equal"<< endl;

As you see if the condition is true, it will print out the text. My problem is when the condition is false! The statement is still executed.
Last edited on
What types are variable1 and variable2?
integers.
no, if the condition is false the statement will not be executed (obviously).
I know that...for some reason the false condition is executed within my Dev-Cpp IDE.
Post the actual code and the five lines preceding and succeeding the conditional. Don't forget to enclose it within [code] and the closing code tags.


Last edited on
Maybe in real code you placed ; after if. That is your real code looks like

if(variable1 == variable2) ;
cout<<"Both numbers are equal"<< endl;
Last edited on
I am going right out of the book and I can't see why it wouldn't work in my IDE.



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


int main()
{ 
    int number1, number2;
    
    cout<<"Enter two number to test their equality.. "<< endl; 
    cin>>number1, number2;
    

    if(number1 == number1) 
               cout<<"Both numbers are equal" <<endl;
   

     
    if(number1 != number2) 
               cout<<"Both numbers are not equal."<<endl;

    
    if(number1 > number2) 
               cout<<"First number " << number1 <<" is greater than your second number "<< number2 << endl; 

    
    if(number1 >= number2) 
               cout<<number1<<" greater than or equal than "<<number2 <<endl;

               
    if(number1 < number2)              
              cout<<number1<<" less than "<<number2 <<endl;

               
    if(number1 <= number2)
               cout<<number1 <<" less than or equal to "<<number2 <<endl;
               

 
    system("pause");
    return 0;
}
F1LewisH wrote:
within my Dev-Cpp IDE.


Found the problem... Don't use Dev-Cpp, it is old, buggy and outdated. Upgrade to something more reliable, such as CodeBlocks, wxDevCpp or Visual Studio (Windows only).
cin>>number1, number2;

doesn't do what you think it does.

cin >> number 1 >> number 2;
Your mistake is in the statement

cin>>number1, number2;

In this statement the comma operator is used. That is the statement contains two expressions. One is cin>>number1 and the second is number2.
So you enter nothing for number2. To correct your statement you should write

cin>>number1 >> number2;
Last edited on
It must be my IDE... the result is still the same. Thanks for the help.
Topic archived. No new replies allowed.