Thread: Can someone help me please?

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's not a matter of true or false, there is no testing involved in the assignment.
    Well that's not true. Anywhere you can write an if statement, you can also do:

    Code:
        string var = "iEatBananas";
        bool flag = var == "iEatBananas";
        if (flag) ...
    What he really needs to do is write some loops. The simplest tasks can be surprisingly complex and would force him to use what he is trying to learn.

  2. #17
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    Quote Originally Posted by skyliner View Post
    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.


    thank you so much for that best answer iv got up to now.... So to be honest i don't even need to use them really

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Ryan500 View Post
    thank you so much for that best answer iv got up to now.... So to be honest i don't even need to use them really
    Well, Boolean as a type may not be useful, but you absolutely do need to know how to read Boolean expressions. You have to understand the logic behind the decisions the computer needs to make. Where you choose to use bool in my experience is completely optional (though, I have done it).

  4. #19
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    ok do you have to use bool to exit a program? Is this how to do bool?


    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
          bool number = !0;
          
         
        
         cout <<"Is number 1 your number :" << number << endl;
         cin >> number;
         
         if (number == 'y')
         {
                    cout <<"This is true" << endl;
                    }
               if (number != 'y')
               {
                          cout <<"This is false" << endl;
                          }
        
        
        system("pause");
    }
    Last edited by Ryan500; 07-16-2012 at 04:07 PM.

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    ok do you have to use bool to exit a program?
    Yes you do. Under normal circumstances, there are many paths of execution a program can feasibly take. Suppose your program is supposed to read a file, so you open it, and the file handle is useless because the file isn't there. A proper course of action would be to first show the error and then exit the program. There is a Boolean test involved in this process.

    So main() contains this:
    Code:
    string file_path = "important.txt";
    fstream file (file_path.c_str(), ios::in);
    if (!file.is_open())
    {
        cerr << "The file " << file_path << " could not be opened for reading.\n";
        return 1;
    }
    else
    {
       // successful opening file
       // do program stuff here
    }
    return 0;
    Not good enough?

    There is an entire thing called event driven programming which among other things decides to quit the program only when the quit program event occurs. This simple strategy can be used for any program where you might want to do more than one thing before quitting. You'll probably write a program like this if you keep studying.

    I have yet to see a program with any use really involve no decision making, even if it is just deciding when to exit the program. Therefore, boolean is important.
    Last edited by whiteflags; 07-16-2012 at 04:15 PM. Reason: minor speeling errors

  6. #21
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    ok thank you im getting there now i think , could you look above yours please and see if that peice of code is right for boolean

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's not right. I will fix it for you since this might be instructive.\

    Code:
    #include <iostream>
    #include <iomanip> // for boolapha
    using namespace std;
    int main ()
    {
        bool number = true; // same as !0 but less hard to read
    
        cout <<"Is number 1 your number :" << number << endl;
        cin >> boolalpha >> number;
        // boolapha allows the user to type true and false as answers instead
    
        if (number == true)
        {
            cout <<"This is true" << endl;
        }
        else // What to do when the if block isn't entered
        {
            cout <<"This is false" << endl;
        }
    
        // system("pause"); // I don't need this...
                            // If you do - #include <cstdlib> please
    }
    That isn't to say you're on the wrong track. If you really want the user to type 'y' or 'n' -- then bool is not the correct type. Use char for characters. There will still be a check for whether the user typed 'y' or not, the difference is surprisingly small:
    Code:
    char isNumber;
    cin >> isNumber;
    if ( isNumber == 'y' ) 
    {
        cout << "This is true" << endl;
    }
    else
    {
       cout << "This is false" << endl;
    }
    I cut down my example to show the difference but you can cut and paste these in the previous program.

    Your reaction is probably confusion. Because we used the == operator, it turns the whole expression into a Boolean expression: isNumber == 'y' can only result in true or false. So in this way, we are not using bool variables but we are using "bool" expressions. I hope this establishes that bool expressions are important, even if you don't use explicit variables. You can if you wanted to assign isNumber == 'y' to a bool variable, and then test that variable. That would only be because you deem it to be easier to understand than what you would have written otherwise.
    Last edited by whiteflags; 07-16-2012 at 04:42 PM. Reason: speeling again

  8. #23
    Registered User
    Join Date
    Jul 2012
    Posts
    30
    ok i get it now at last lol , thank you so much for your help i will learn of that code you have done. Very well explained. Thanks.

  9. #24
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Good luck with this - as whitefalgs says, its really very important to get your head around logic - it is basically what allows you to divert your program flow - without it well it will just run in a linear fashion and not really be able to do much. In an if(..) statement no matter what data types you write be they bool, char, int, whatever, the evaluation or 'answer' always comes down to boolean logic anyway - you could write the most complex thing ever inbetween those (brackets) but it will always be evaluated as either true or false in order to continue - thats why you can also just write shorthand without using the == value

    Code:
    //
    //
    bool DoCalculation(int x)
    {
        //do calcs...
        //do some more calcs...
        //retun true or false depending on outcome of calcs..
    }
    
    int main()
    {
        
    bool calculate = true;
    int x = 5;
    
        while(calculate)
        {
            if(!DoCalculation(x))
            calculate = false;
        }
        cout << "finished calculating x!\n";
        
     return 0;
        
    }
    looking at that you might be able to work out a much shorter way to accomplish the same steps..
    Last edited by rogster001; 07-22-2012 at 05:16 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  10. #25
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Code:
    #include<iostream.h>
    int main()
    {
    	int a=10,b=200;
    	bool result=a<b;
    	cout<<result;
    	if(a<b)
    	{
    	    //some 100 lines code
    	}
    	  //some 100 lines of code
    	if(a<b)
    	{
    		if(a<b)
    		//again code
    		if(a<b)
    	}
    	    //code more code...
    	
    	
    }
    just have a look at the above code.imagine if you have to check if a<b a 100 time in your code and somewhere instead of a<b, you write a>b..such a logical error can take time to spot..
    instead see the code below
    Code:
    #include<iostream.h>
    int main()
    {
    	int a=10,b=200;
    	bool result=a<b;
    	cout<<result;
    	if(result)
    	{
    	    //some 100 lines code
    	}
    	  //some 100 lines of code
    	if(result)
    	{
    		if(result)
    		//again code
    		if(result)
    		
    	}
    	    //code more code...
    	
    	
    }

    this is something i have used personally.hope this helps

    a bool variable can have two values "true"(non zero integer) or "false"(zero)...it can be used to store results of logical operations as above

  11. #26
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    nice topic bro
    gonna due some reading my self

Popular pages Recent additions subscribe to a feed