Thread: Can someone help me please?

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

    Can someone help me please?

    Hi , im really finding it hard to understand the bool. Can someone give me a example of how expierence programmers would use it please. Looked at the tutorials and there all the same.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ryan500
    im really finding it hard to understand the bool. Can someone give me a example of how expierence programmers would use it please.
    What is bool, and what are your examples of it being used?
    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

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    anything really so i can understand it. Just finding it really hard to understand

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but as I cannot read your mind, I cannot help you. Basically, I can tell you a whole lot of things, but if they are just repeating what you failed to understand, I would be wasting my time. Hence, I want to know what you already know, or at least think you know. If you really cannot tell me that, then either you did not make an effort to learn, or you're really not cut out for programming.
    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

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    everyone is cut out for programming just depends if there into it. Up to now i only no how to do this :

    Code:
    bool test = true;
    bool test = !false;

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ryan500
    Up to now i only no how to do this :
    Code:
    bool test = true;
    bool test = !false;
    What do these two statements do?
    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

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    First line make it true second line makes it false but i still don't get how to use it. Like what do you use it for etc...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Okay, imagine that you were writing some code to check if a number exists in a collection of numbers. You could start by setting a bool variable named found to false, then loop through the collection until you found the number, upon which you set found to true and stop looping. Thus, after the loop has ended, you can determine if the number was found by checking the value of the found variable.

    This is just one example; this margin is too narrow for me to list all of them.
    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

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    oh ok so could you tell me if this is right?

    Code:
        bool number = 10;
        bool number2 = !false;
        
        if (number == number2)
        {
                    cout <<"This is your number : " << number << endl;
                    cout <<"This is a false number : " << number2 << endl;
                    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Whether it is right or wrong depends on what you are trying to do.

    That said, why do you do this?
    Code:
    bool number = 10;
    As you should know, there are two possible boolean values, namely true and false. 10 is not one of them. Now, 10 will be converted to true, but did you really intend that?
    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

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    no didnt intend to do that , its hard to understand

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Um, given laserlight's task, I would expect the collection of numbers to be something like int collection[N];

    You would look in there for your number, going one element at a time, I'd expect.

    As for the boolean called found, even though there is no found in your program, I would not do this.
    Code:
    bool found = !false;
    !false uses the NOT operator (!) which makes the result the opposite. So now found is actually true. It would be better to be as simple as possible. The only time I've used NOT explicitly was actually in complex loop conditions. Sometimes using NOT is the simplest expression, most times not.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I suggest that you continue with your learning. As you go on, ideas on how this fits together will come naturally. I could of course give you examples with loops, arrays, etc, but if you don't know them yet, you're just going to get more confused.
    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

  14. #14
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    iv been learning c++ for a while but iv been on and of cause i find it hard to learn cause i have learning difficulties

  15. #15
    Registered User
    Join Date
    Jul 2012
    Posts
    23
    Ryan500;
    Think of it this way:
    A boolean variable, or bool for short, is nothing more than a special integer variable that can only hold two values: 1 or 0.

    It's not a matter of true or false, there is no testing involved in the assignment. The c++ language uses the keywords true or false, but you do not need to get lost behind the meaning of the words. It could be Up or down, left or right on or off, etc. It doesn't matter. What matters is that the variable either has a value(1) or is empty(0).

    That is all, a simple variable that can only hold two values.
    In reality you do not even need to use bools. You can achieve the same results using other types of variables, int, floats or whatever:
    e.g
    Code:
    int var = 0;
    
    if (var == 1)
        doSomething;
    else if (var == 0)
        doSomethingElse;
    is exactly the same as:

    Code:
    bool var = false;
    
    if (var == true)
        doSomething;
    else if (var == false)
        doSomethingElse;
    or
    Code:
     string var = "iEatBananas"; 
    
    if (var == "iEatStrawberries")
        doSomething;
    else if (var == "iEatBananas")
        doSomethingElse;
    In all those examples the code below the 'ifs' will only run if 'var' is equal to whatever you are comparing it to., be it:
    Code:
     if (var == "airplane"); if ( var == 789);  or the dreaded  if (var == true);
    I hope that helps.

    I apologize for the blunder and inaccuracy of my oversimplified statements. I was just trying to explain the concepts in a simple, accessible manner.

Popular pages Recent additions subscribe to a feed