I need a better explanation about 'bool'.

So i'm currently a c++ starter as in i'm really a completely in programming i have no programming experience and bool confuses me.
That's why i came here looking for better explanation about bool.
Not a native english speaker so sorry about my grammer.

As far as I know if statements is a boolean operator true and false.
I know 'if ( guess == answer )' in general language means
'if guess is equal to answer then prints whatever inside the bracket'

I also know variable declaration and defining a variable. But this part really
confuses me. Not sure what it means.

 
bool notguessed = true;


 
notguessed = false;


This is not my code by the way i just copied it from other tutorials site and trying to understand programming language.


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

int main()
{
    int answer = 45;
    int guess;
    bool notguessed = true;
    while(notguessed)
    {
        cout << "Guess my number: ";
        cin >> guess;

        if (guess == answer)
        {
            cout << "Yes! " << endl;
            notguessed = false;
        }
        if(guess < answer)
        {
            cout << "Too low..." << endl;
        }
        if(guess > answer)
        {
            cout << "Too high... " << endl;
        }
        if(guess >= 100)
        {
            cout << "Too damn high..." << endl;
        }
        if(guess <= -1)
        {
            cout << "Too damn low..." << endl;
        }

    }
}
Last edited on
I fixed a few part of your code

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
#include <iostream>
using namespace std;
int main(){
    int answer = 45;
    int guess;
    bool notguessed = false; //Give a false instead.
    while(notguessed != true){ //While notguess is not equal to true, if it is, good job and close out.
        cout << "Guess my number: ";
        cin >> guess;
        cin.sync();
        cin.clear();

        if (guess == answer){
            cout << "Yes! " << endl;
            notguessed = true;
        }else if(guess < answer){
            cout << "Too low..." << endl;
        }else if(guess > answer){
            cout << "Too high... " << endl;
        }else if(guess >= 100){
            cout << "Too damn high..." << endl;
        }else if(guess <= -1){
            cout << "Too damn low..." << endl;
        }else{
			cout << "Your input was not a value...";
		}
    }
	return 0;
}


bool stands for boolean, you either use true or false. Where false = 0 and true = 1. Ever heard of binary codes? where you read only the 1's and add them up leaving alone the 0's. Well basically booleans are used for testing like how you tested your code to see if the user guessed the right value.
Last edited on
A bool is a data type... just like an int.

An int can hold an integer number anywhere between a certain range (several billion). However a bool can only hold 2 values: "true" and "false"

So go back to basics:

 
int var = 5;

This creates an integer variable, and puts a value of 5 in it.

Similarly:
 
bool notguessed = true;

This creates a bool variable and puts a value of 'true' in it.
Hi,

Also realise that relational statements (Which involve relational operators such as == , != , < , <= , && , || etc) evaluate to a logical true or false (aka boolean), which the compiler can use to do conditional flow control like if, loops, switch etc.

There are also unary operators like & | ^ ! which operate on an integral type number, producing a different number which would be assigned to some variable.

All of these can be used to do flow control, as in:

1
2
3
4
5
bool Quit = false;

while (!Quit) {
    // do something
}


Beginners sometimes confuse the difference between a bool type and boolean evaluation of statements.

They are called bool because Mr George Bool was apparently the one who first invented this branch of mathematics involving 0 and 1 with AND, OR, NOT XOR etc operations


Hope this helps a bit :+)
Topic archived. No new replies allowed.