Like Tree1Likes

Can someone help me please?

This is a discussion on Can someone help me please? within the C++ Programming forums, part of the General Programming Boards category; Hi , im really finding it hard to understand the bool. Can someone give me a example of how expierence ...

  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
    20,955
    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?
    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

  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
    20,955
    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.
    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

  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
    20,955
    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?
    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

  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
    20,955
    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.
    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

  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
    20,955
    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?
    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

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

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    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
    20,955
    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.
    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

  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
    17
    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.
    rogster001 likes this.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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