Using char with bool

This is a discussion on Using char with bool within the C++ Programming forums, part of the General Programming Boards category; Hi , how would i use char with bool here is the code : Code: bool buy = true; 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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    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
    21,401
    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++?"
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:18 AM
  2. Replies: 3
    Last Post: 11-15-2009, 03: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, 10:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21