Thread: Finding it hard to understand boolen

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

    Finding it hard to understand boolen

    Hi , im finding it really hard to understand how boolen works
    and how it supposed to be used. Iv read all tutorials and i still cant understand any help please.

    Thanks

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's a truth value. It can be true or false. What part do you have problems with? Logical operations? If statements? Loops?

    It's boolean, by the way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    im having a problem understanding on how to write a code with it up to now iv done this. But when the user enters coke how do i get it to say you have a bought a coke.

    Code:
    string coke1 = "coke";
    bool coke = true;
    bool cokef =(false);
    cout <<"Enter coke to buy" << endl;
    cin >> coke1;

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Remove the lines declaring the two bool variables, and add this after the cin line.
    Code:
    bool typed_coke = (coke1 == "coke");
    if (typed_coke)
        cout << "You typed in coke\n";
    else
        cout << "You typed in something else\n";
    
    cout << "Another way that is useful sometimes, although it can make code harder to understand\n";
    
    cout << "You typed in " << (typed_coke ? "coke" : "something else") << '\n';
    Last edited by grumpy; 07-14-2012 at 07:43 PM. Reason: Incorporate correction from King Mir in later post
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    thats a bit complicated for me as im a beginner

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The amount of spoon-feeding on offer here is finite.

    The first four lines in the code fragment I gave are an absolute minimum that a beginner needs to work with booleans.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    what dose typed mean i havent come across that yet

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In the sense that I used it ....
    Quote Originally Posted by English language dictionary
    type (verb) : to write using a typewriter or keyboard.
    The word suffix "ed" has the effect of "forming the past tense of a weak verb". In other words it converts the present tense "I type this post" into the past tense "I typed this post".

    There is an assumption here that you understand the english language, or will look up basic things like that if you don't understand.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by grumpy View Post
    Remove the lines declaring the two bool variables, and add this after the cin line.
    Code:
    bool typed_coke = (coke1 == coke);
    if (typed_coke)
        cout << "You typed in coke\n";
    else
        cout << "You typed in something else\n";
    
    cout << "Another way that is useful sometimes, although it can make code harder to understand\n";
    
    cout << "You typed in " << (typed_coke ? "coke" : "something else") << '\n';
    I think you want:
    Code:
    bool typed_coke = (coke1 == "coke");
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Thanks for the correction, King Mir. I'll edit my preceding post accordingly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    best thing I've learned (as I am a beginner too) is if you don't understand something, you may not be ready to tackle it yet.. I've often gone back and re learned the things I already knew, until whatever I was struggling with just kinda clicked..
    Boolean only has 2 options- true or false.
    also the people on the forums are very helpful, but don't expect them to complete your code for you through every step.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with detecting/finding/accessing a hard disk with C
    By bemyzinger in forum C Programming
    Replies: 2
    Last Post: 12-10-2011, 02:05 PM
  2. Hard Time finding .NET books
    By mrafcho001 in forum C# Programming
    Replies: 1
    Last Post: 03-01-2006, 06:41 AM
  3. Finding the smallest value......HARD!!!
    By AssistMe in forum C Programming
    Replies: 21
    Last Post: 03-10-2005, 06:23 AM
  4. finding capacity of hard drive
    By jverkoey in forum Windows Programming
    Replies: 4
    Last Post: 08-20-2003, 07:21 AM
  5. 'Installing' to hard drive and 'Saving' to hard drive
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2002, 02:37 PM