Why is my boolean returning an integer?

I am new to the programming world, and I had a school assignment to create a simple program containing an if loop. Basically, I want my code to return true if a is greater than 18, and false if a is less than 18. I also want a to increase by one each time canTakeRoadtest returns false. When I run my code, all I get is

"I'm 0 years old.

I can pass my road test. 0 "

I am wondering why bool canTakeRoadtest returns an integer and why a does not increase. Any help would be appreciated, and I apologize for the simple question, but we all start somewhere!

My coding environment is Eclipse Neon, if it has any meaning.

#include <iostream>

using namespace std;

int main() {
//Local Variables
    int a = 0;
    bool canTakeRoadtest = false;
//If Loop
    if (a >= 18) {
        canTakeRoadtest = true;
        cout << "I'm ready!\n";
    } else {
        canTakeRoadtest = false;
        cout << "I am " << a << " years old.\n";
        cout << "I can take my road test. " << canTakeRoadtest << endl;
        a++;

    }

    //return 0;

}
+4
source share
2 answers

Behind the scenes, each type of variable in C ++ has a numerical representation. This makes sense, because at the basic level of the computer, all that is is numbers. There are two basic primitive data types that (as a rule) are represented as something other than a number, but are actually just numbers. The first is bool , and the second is char .

For example, if you installed

bool myBool = true;

behind the scenes, it really sets your variable to 1 . Similarly, if you install

myBool = false;

0 . .

char myChar = 'a';

char 97. , , table.

, cout bool, "", , -, iostream , bool, "" . . iostream.

iostream, ,

cout << "I can take my road test. " << canTakeRoadtest << endl;

cout << "I can take my road test. false" << endl;

boolalpha, Lovelace42.

+3

boolalpha. bool , boolaplpha bool

http://www.cplusplus.com/reference/ios/boolalpha/

cout.

if (a >= 18) {
    canTakeRoadtest = true;
    cout << "I'm ready!\n";
} else {
    canTakeRoadtest = false;
    cout << "I am " << a << " years old.\n";
    cout << "I can take my road test. false" << endl;
    a++;
}
+2

Source: https://habr.com/ru/post/1673795/


All Articles