Application of bool variable question!! ( Appreciate if you could answer this)

I've been reading up the c++ for dummies guide and I've come to the section of demonstrating the bool variable.

So the code from the book goes like this

(Sorry if this is like a tl;dr!!)


// BoolTest - compare variables input from the
// keyboard and store the results off
// into a logical variable
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// set output format for bool variables
// to true and false instead
// of 1 and 0
cout.setf(cout.boolalpha);
// initialize two arguments
int nArg1;
cout << “Input value 1: “;
cin >> nArg1;
int nArg2;
cout << “Input value 2: “;
cin >> nArg2;
bool b;
b = nArg1 == nArg2;
cout << “The statement, “ << nArg1
<< “ equals “ << nArg2
<< “ is “ << b
<< endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system(“PAUSE”);
return 0;
}

Now, when I run the program eg. it's like this.
Input value 1: 5
Input value 2: 5
The statement, 5 equals 5 is true
Press any key to continue . . .




HOWEVER, when I tried adding more variables like these, (I'll skip the common parts so it's not so long-winded)

int var3;
cout<<"Input value 3:";
cin>>var3;

int var4;
cout<<"Input value 4:";
cin>> var4;


bool lol;
lol = var1 == var2 == var3 == var4;

cout<<"The statement,"<<var1<<"equals"<<var2<<"equals"<<var3<<"equals"<<var4<<"is"<<lol<<endl;
system("PAUSE");
return 0;
}

and this time if I try to run the program, eg,
Input value 1:5
Input value 2:5
Input value 3:5
Input value 4:5
The statement, 5 equals 5 equals 5 equals 5 is !!>FALSE<!!
Press any key to continue . . .

I hope you understand what I'm trying to say! It says true for 2 variables of the common digit however if I decide to add in more variables, it suddenly becomes false! I'm curious to know why and I would really appreciate advice and explanation! (I'm a newbie sorry, p.s. I apologize for the really long post, my first time posting here.)

Thank you in advance!
Try this;
 
lol = (var1 == var2) && (var2 == var3) && (var3 == var4)

Last edited on
Thank you very much quine! But I'm extremely puzzled as to why it matters to have () between the different variables and


also,

bool lol;
lol = var1 == var2 == var3 != var4; -> When I add an inequality (!=), and when I run the program whatever number I type for the variables, the statement, eg. 1 equals 1 equals 1 equals 1 is TRUE

why is that so?
receipter wrote:
I'm extremely puzzled as to why it matters to have () between the different variables

The parentheses are not important here. It's the use of && that makes the difference.

First you need to understand what the operators do.
The == operator returns true if the two operands are equal, otherwise it returns false.
The && operator returns true if both operands are true, otherwise it returns false.

If you have something like var1 == var2 == var3 it's the same as (var1 == var2) == var3. First the expression var1 == var2 is evaluated and the result is a bool. Then this value is compared to var3 and the result is yet another bool. Note that when you compare a bool with an int, true will be treated as 1 and false as 0.

A simple example to demonstrate: var1 = 5, var2 = 7, var3 = 0.
var1 == var2 == var3
(var1 == var2) == var3
(5 == 7) == 0
false == 0
true
Last edited on
Thank you Peter87 for your response, just a part which I can't really grasp
false == 0
. If I were to put var3 = 1 instead of var3 = 0, would the final outcome be false as a result of that?

And also if you could help me on this one, where I previously mentioned about adding an inequality (!=) to that line, why does the result produce a true instead of a false? (I think I'm almost getting it, but I can't piece the puzzle together!)

Would really be grateful for your advice!
receipter wrote:
false == 0
. If I were to put var3 = 1 instead of var3 = 0, would the final outcome be false as a result of that?

Yes.

receipter wrote:
And also if you could help me on this one, where I previously mentioned about adding an inequality (!=) to that line, why does the result produce a true instead of a false?

It's the same logic, only that != returns the opposite of what == returns.

You don't need to think too much about using == and != in row like this because that is very seldom useful. Instead you use operators like && or || to chain multiple comparisons together, as shown by quine.
Alright thank you very much! Case closed ha ha
Topic archived. No new replies allowed.