Thread: Using char with bool

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    30

    Using char with bool

    Hi , how would i use char with bool here is the code :


    Code:
       bool buy = true;
       bool dbuy = false;
        
        
        cout <<"Do you want to buy a can of coke? : " << endl;
        cin >> buy;
        
        if (buy = 'yes')
        {
                cout <<"Bought!" << endl;
                }
                if (dbuy = 'no')
                {
                         cout <<"No sale!" << endl;
                         }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need a string in this case

    string buy;
    cin >> buy;
    if ( buy == "yes" )
    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.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    where dose the bool come in



    Code:
     string buy = true;
       string dbuy = false;
      
        
        
        cout <<"Do you want to buy a can of coke? : " << endl;
        cin >> buy;
        
        if (buy == 'yes')
        {
                cout <<"Bought!" << endl;
                }
                if (dbuy == 'no')
                {
                         cout <<"No sale!" << endl;
                         }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well that's what comparisons return

    As in

    bool anAnswer = ( buy == "yes" );

    If you look at anAnswer, it will either be true or false.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    ok i did it but when i type yes or no it comes up with both answers no sale and bought


    Code:
      
        bool answer = ( buy == "yes" );
        {
                cout <<"Bought!" << endl;
                }
                 bool answer2 = (dbuy == "no");
                {
                         cout <<"No sale!" << endl;
                         }

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You need to use an if statement at some point. You know, like the one you had minutes ago?

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    where do i put it

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Exactly where you had it before.

    Do you know all the basic types in C++? They can all be compared. That means that they can all be used in a Boolean expression, and the explicit use of a bool variable is optional.

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    yes i know a little still something wrong...


    Code:
        if bool anAnswer = ( buy == "yes" );
        {
                cout <<"Bought!" << endl;
                }
                else if bool anAnswer2 = ( dbuy == "no" );
                {
                         cout <<"No sale!" << endl;
                         }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need a beginner's book. I'd recommend Accelerated C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    very helpfull

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    How did this thread go from (almost) correct to horribly wrong? All you needed to do to use boolean logic was to replace "=" with "==" in the if statement. You could have used temporary variables to hold this value, but either way, you're using boolean. For example:
    Code:
    bool r = (buy == "yes");
    if (r) {
        /* do stuff */
    }
    is the same as:
    Code:
    if (buy == "yes") {
        /* do stuff */
    }
    when a comparison in C/C++ is just an expression that evaluates to either zero or one depending on if it's false or true, respectively.

    EDIT: Looks like this is just a continuation of your confusion regarding boolean. You should really look for a tutorial on boolean logic and its usage in C before you post more code.
    Last edited by memcpy; 07-18-2012 at 08:54 PM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by memcpy
    when a comparison in C/C++ is just an expression that evaluates to either zero or one depending on if it's false or true, respectively.
    In C++, the comparison would evaluate to false or true, not zero or one. Also, be wary of "C/C++": Stroustrup on "What do you think of C/C++?"
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedefining the BOOL as unsigned char
    By ushacy in forum C Programming
    Replies: 8
    Last Post: 11-17-2011, 05:18 AM
  2. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  3. BOOL bool ? unresolved external symbol
    By xwielder in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 08:39 AM
  4. How get bool's from std::vector<bool>
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 05:24 AM
  5. Interesting: bool != BOOL
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2002, 11:09 AM