Bool not working as intended

Jun 27, 2014 at 9:56am
Hello, I have just started practicing with 'bool' and for some reason it is not working at all, and is doing the opposite of the intended function.

#include <iostream>
#include <vector>
using namespace std;


int main ()

{
cout << "Please enter two numbers" << endl;
int a , b;
cin >>a;
cin >> b;
bool equal = (a==b);
cout << "Result " << equal << endl;
bool notequal = (a!=b);
cout << "Result " << notequal << endl;
return 0;
}

From my understanding what is supposed to happen is (for example, entering 3 and 4) I get a 1 for 'equal' indicating it was not true and a 0 for 'notequal' indicating it was true. Instead, for example, when entering the numbers 3 and 4, I get a 0 for equal and a 1 for notequal, and it is very frustrating. I'm not sure what is wrong, and any help would be greatly appreciated. Thank you.
Jun 27, 2014 at 10:22am
closed account (1v5E3TCk)
You need to use an if statement there. you say if a equals to b boolean equal is true else it is false and if a not equals b boolean notequal is true else it is false. You dont check values of your booleans.

Do it like that:

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

int main ()

 {
       cout << "Please enter two numbers" << endl;
      
       int a , b;
      
       cin >>a;
       cin >> b;
      
       bool isEqual = (a==b);

       if( isEqual )
       {

              cout<< "They are equal";

       }

       else
              cout>>"They are not equal";

       return 0;
 }
Jun 27, 2014 at 10:22am
Actually, it's doing the right thing, it's your expectations that are opposite.

a "1" in this case means "true" and 0 means "false" I've read of but never come across (I'm sure they exist though) any scheme that uses "0" for true and "1" for false.

Better yet, instead of
1
2
 cout << "Result " << equal << endl;
 cout << "Result " << notequal << endl;

try
1
2
3
 
cout << "Result " << boolalpha << equal << endl;
cout << "Result " << boolalpha << notequal << endl;

javascript:editbox1.editSend()
you'll see the answers as "true" and "false" instead of "1" and "0"

Next time, format your code, and from now on, assume the compiler is right and you are wrong :-)
Jun 27, 2014 at 10:33am
Perhaps printing "true" or "false" along with 0 or 1 would help illustrate how the bool works?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;


int main ()

{
    cout << "Please enter two numbers" << endl;
    int a , b;
    cin >>a;
    cin >> b;
    bool equal = (a==b);
    cout << "Result (a == b) " << boolalpha << equal << " " << noboolalpha << equal << endl;
    bool notequal = (a!=b);
    cout << "Result (a != b) " << boolalpha << notequal << " " << noboolalpha << notequal << endl;
    return 0;
}


Jun 27, 2014 at 10:51am
senhor: Thanks for the suggestion on an if statement, but I am yet to reach the if statement part of the book yet so I was unable to use it here
tipaye: I really appreciate the help, I genuinely though 0 was seen as true because you usually return 0 at the main function, and I thought that had something to do with it being true. Anyway, I've realized my mistake. I knew the compiler couldn't seemingly work but give me strange results in that way.
jlb: Thanks for the new 'boolalpha' and 'noboolalpha' to use, they definitely make it easier to understand and would have told me right away if I was correct.
Thanks to everyone for the help.
Topic archived. No new replies allowed.