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.
#include <iostream>
usingnamespace 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;
}
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).
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