Thread: Boolalpha function

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13

    Boolalpha function

    Hi guys. I'm new to programming. I have a question. I've seen a book teaching C++ and here's the code they gave:

    Code:
    // This programme shows an example of bool data type
    #include <iostream>
    using namespace std;
    
    int main()
    {
        bool excellent = true, good = false;
        
        cout<< "The value of excellent is "<< boolalpha << excellent<< endl;
        cout<< "The value of good is "<< boolalpha<< good;
        cin.get();
    }
    It seems that when I didn't add the boolalpha in
    Code:
    cout<< "The value of good is "<< boolalpha<< good;
    , it still display the true and false. Why is that so? Can anyone help me?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The boolalpha flag, once set, has effect as long as it is not unset with "noboolalpha".
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    21
    boolalpha can display true ou false
    nonboolalpha display 1 or 0

    if you didn't add boolalpha it display true ou false

  4. #4
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13
    Quote Originally Posted by anon View Post
    The boolalpha flag, once set, has effect as long as it is not unset with "noboolalpha".
    Is that true? So is it just applicable to just a function? For example, if I put boolalpha in main function, can it last until another function prototype?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Assuming cout is a global object, once you set a permanent format flag somewhere, it will take effect from then on everywhere until you reset it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13

    Unhappy

    Quote Originally Posted by anon View Post
    Assuming cout is a global object, once you set a permanent format flag somewhere, it will take effect from then on everywhere until you reset it.
    lol...I don't know what you mean...because cout will never be a global object? I'm new to programming, sorry for my newbiness

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by junkeat90 View Post
    lol...I don't know what you mean...because cout will never be a global object? I'm new to programming, sorry for my newbiness
    No, normally, cout _IS_ a global object. You can of course have a local cout, if you do something like:
    Code:
    void somefunc(ostream &cout)
    {
        cout << "Some text" << endl;
    }
    
    int main()
    {
       ofstream myoutput("somefile.txt");
       somefunc(myoutput);
       return 0;
    }
    [That is not a complete piece of code, but should be sufficient to illustrate the case - and if it makes you feel like "???!", then ignore the whole part of the discussion of cout being a local object and see cout as "always global"]
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM