Thread: reading a bool

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    reading a bool

    I can save a bool value but I get an error when I try to read it ->

    bool dead;

    save<<player.dead<<endl; //saves it as 0, this works fine

    save>>player.dead; //Gets the following error - binary '>>' : no operator defined which takes a right-hand operand of type 'bool' (or there is no acceptable conversion
    My site to register for all my other websites!
    'Clifton Bazaar'

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    Read it in as a char and convert it to bool, it should work the same since a bool is still a byte.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One option is to open the stream for input and output.

    Code:
    #include <fstream>
    using namespace std;
    
    struct player_type
    {
       bool dead;
    };
    
    int main()
    {
       bool dead;
    
       player_type player;
       player.dead = true;
       //player.dead = false;
    
       fstream save("game.txt",ios::in | ios::out);
       save<<player.dead<<endl; //saves it as 0, this works fine 
       save>>player.dead; //Gets the following error - binary '>>' : no operator defined which takes a right-hand operand 
    
       cout << "player.dead:" << player.dead << endl;
       if (player.dead)
          cout << "dead\n";
       else
          cout << "alive\n";
       return 0;
    }
    Last edited by swoopy; 10-05-2001 at 10:50 PM.

  4. #4
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    thanks no-one, I'll do it that way. I will admit onething to you no-one, that was a brilliant creative thought to get your name, it deflates the ego when you get an e-mail that reads -> 'no-one has just replied to your thread'

    fstream save("game.txt",ios::in | ios: ut); // dont you hate it when the face comes up in the code?

    Cheers for the reply swoopy. The code I gave assumed that I had the streams open for input and output. I was able to output the bool (it is either 0 or 1) but I couldn't read it in as a bool
    My site to register for all my other websites!
    'Clifton Bazaar'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesn't this example work for me?
    By xixpsychoxix in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 08:25 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Problem displaying bitmaps
    By batman123 in forum Game Programming
    Replies: 2
    Last Post: 01-09-2005, 02:01 AM
  4. bool vs BOOL
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 04-10-2004, 09:55 AM