Thread: Using boolalpha to print true and false but a little confused here

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Using boolalpha to print true and false but a little confused here

    I'm using the iomanip header and boolalpha to print the words "true" and "false". I have a program that demonstrates a simple use of boolean variables. It goes like this:

    Code:
    #include <iomanip>
    #include <conio.h>
    using namespace std;
    
    int main()
    {
        bool b;
        
        b = false;
        
        cout << "b is " << b << endl;
        cout << "In other words, b is " << boolalpha << b << endl << endl;
        
        b = true;
        
        cout << "b is " << b << endl;
        cout << "In other words, b is " << boolalpha << b << endl << endl;
        
        if(b)
            cout << "Karen has a boatload of problems, but she's not all bad." << endl;
            
        b = false;
        
        if(b)
            cout << "Jerry is a nice guy...but actually that's a lie" << endl;
            
        cout << "13 > 11 is " << (13 > 11);
        
        getch();
        return 0;
    }
    When I run it the first two cout statements look fine:

    b is 0
    In other words, b is false

    No problem there but when I run the next two cout statements I see this:

    b is true
    In other words, b is true

    So I'm wondering why the compiler is not printing "b is 1" before I specifically instruct it to print "b is true" in the following statement?

    I'm guessing that when I use boolalpha for the first time in the cout statement, that is setting it for the rest of the program? Do I need to shut it off before I use it again?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. True or False
    By camel-man in forum C Programming
    Replies: 9
    Last Post: 02-14-2013, 01:27 AM
  2. The 1 and 0.. True/false - CONFUSED!
    By Crazycanook in forum C++ Programming
    Replies: 12
    Last Post: 07-07-2011, 02:13 AM
  3. Help with True, False, NOT, AND, OR...
    By cgsarebeast in forum C++ Programming
    Replies: 22
    Last Post: 05-09-2006, 07:59 PM
  4. 1 or 0 == true or false?
    By Discolemonade in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2005, 04:08 PM
  5. Return True or False, Not Print
    By BB18 in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 02:51 PM

Tags for this Thread