Hi. I'm new to c++ and bought the Jumping in C++ ebook. I read about the bool type but was confused about what it is and when I used it.Please help me.
Hi. I'm new to c++ and bought the Jumping in C++ ebook. I read about the bool type but was confused about what it is and when I used it.Please help me.
I am also new to C++, just crossing over from C and basically how I understood it bool is just true / false values instead of 1 / 0.
In practice so far I never used it because you can implement simple integers and check for the value through if statement.
Here is an example how you could do use it, in C you got srand() function which is used to initialize a semi-random number generator and you only need to do it once within your whole program.
If you got that function within your own function something like "int getnumber(....);" and you need to initialize it only once else stuff my not go your way so to speak :P
So you setup an external variable and set it to 0, first time you run srand() you set the variable to 1 and make an if statement that if it's "1" then it means generator has already been initialized and you just continue with
your function/program.
Now you could do the same with bool just using true / false values instead of 1 / 0 but I never used it in practice so some1 else might explain it better :PCode:external int initialized = 0 ; int getnumber(....) { if(initialized != 1) { srand(time(NULL)); initialized = 1; } /*continue your function*/ } int main() { }
A definition is given in Stroustrup's C++ glossary:Originally Posted by etricity
bool - the built-in Boolean type. A bool can have the values true and false.
So, if you have a scenario in which a boolean type makes sense, e.g., a flag or return value to denote yes/no or success/failure, you would consider using bool.
You could, but then if you have multiple such values, the use of an enumerated type (or since C++11, an enum class) would be more appropriate. bool would not be appropriate because the values involved should be restricted to true/false.Originally Posted by Jablan
This is a bad idea: it may be the case that the use of the function wants to seed with a particular value in order for repeatability in testing. Hence, getnumber should not seed, but rather it is up to the caller to seed, e.g., near the start of the global main function.Originally Posted by Jablan
Furthermore, if you really do want to do this, then it is precisely a case where the use of bool is better than your use of int because it is clear that initialized is either true or false, rather than say, 2.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So I should use bool if there are only two possible values, like yes/no or success/fail and I should use int if there are more than two possible values. But what other values are there? And if you are using an if statement aren't there always only two possible values true/false? And can you please give me an example in coding in c++. Thank you for your help guys! I appreciate it!
Last edited by etricity; 03-11-2014 at 03:12 AM.
Like you've been told: if there is a situation which can be described with only true or false, you use a bool.
Did we succeed? Yes or no.
Is the individual female? Yes or no.
Does this computer have more than 8 GB of ram? Yes or no.
Example of where not to use bool:
How much ram does this computer have?
What is the gender of that individual?
What is the status of that function?
An int is an integer - a number. Use it whenever you have a number you must store. It's not more difficult than that.
An if statement branches (i.e. chooses a path) depending on whether the expression is true or false.
Thank You so much guys!! I now understand the difference. Thanks!! You guys are the best!