Cannot understand use of " : "

Jun 22, 2009 at 12:39am
Hi, I'm just beginning to learn C++, only other language I know is Html.I'm trying to use the operator " : " to get a Boolean true or false. I've spent the last hour trying to understand what I'm doing wrong. Here is the code I've done:

#include <iostream>
using namespace std;
int main ()
{
bool True;
bool False;
int a, b, c;
a=2;
b=3+a;
c=(a>b) ? True : False;
cout << c;
return 0;}

Shouldn't I see a result of "False" since b is greater than a? I'm getting the result "227". I have no idea how.
Please help me understand what I'm doing wrong.

Last edited on Jun 22, 2009 at 12:40am
Jun 22, 2009 at 1:01am
You never set True nor False to anything, so they're filled with garbage.

By the way, true and false are keywords with those values. There are also those that don't use them and just use 1 and 0 instead.
Jun 22, 2009 at 10:13am
BTW you can just assign
c = a>b;
Jun 22, 2009 at 10:48am
The point is to understand that bool True; only declares a Boolean type variable called True without ever initializing it, so calling cout << would give......I don't know. The correct keywords are the all lowercase true and false, and you can just use them anywhere without unnecessarily declaring a variable (at least in this simple case) to represent it. C++ is case-sensitive so one may, though it's silly, write bool True = false;.

As long as everything (well, your compiler and you) is standardized, then true gives an integer value of 1 and false 0.

It is funny to try
1
2
int anything;
cout << anything << endl;

If you want the word "True" or "False" to be printed out on the console then you have to write your own code for the cases.
Jun 22, 2009 at 12:48pm
1
2
bool b( false ); 
std::cout << std::boolalpha << b << std::endl;


outputs:

false
Jun 22, 2009 at 5:13pm
Boolean variables may hold any of two distinct values: either true or false.

This is always the case -- it does not matter what you name your variables. Hence:
1
2
3
bool MyAge;
bool True;
bool pi;

These are all misnamed variables, because they don't hold what their names suggest. For example:
1
2
3
bool MyAge = true;   // I have an age?
bool True  = false;  // apparently it is not true...
bool pi    = false;  // maybe circles just don't exist! 


The ?: operator is a shorthand for using an if statement -- with the added side-effect that it returns a value.
http://www.cplusplus.com/forum/lounge/6307/
http://www.cplusplus.com/forum/general/11722/

Hope this helps.
Jun 22, 2009 at 5:34pm
Thanks for all the help! Wow, I can't believe I actually posted a question and got honest answers. First community I've seen in awhile that was this cool. So I can take out the
bool True;
bool False;

and just put
c = a>b ? true : false;.
Thanks again.
Jun 22, 2009 at 5:44pm
Yes. However, as stated before, a>b will also give you the correct boolean answer, so you can simply write c = a>b;.
Jun 22, 2009 at 6:35pm
c is declared as an integer. I wouldn't rely on implicit casting personally. Declare c as a bool as well if you plan on assigning true or false to it. Now if you did this it makes sense for c to be declared as an int.

c = a > b ? a : b; // a, b, c are all int types
Jun 22, 2009 at 6:49pm
Hey

you don't need to use true and false!
just write it:

 
c = a > b;


if condition ( a > b ) is true c = 1 and if ( a > b ) is false c = 0
;)
Jun 22, 2009 at 6:50pm
What? So, now bool isn't an integer? Implicit conversion would be a problem if bool could have negative values (sign extension), but that's not the case.
Jun 22, 2009 at 7:22pm
I didn't say it would necessarily cause a problem Helios but it is poor programming. When doing the reverse assignment compilers might give a warning. bool is not an int. bool and int are completely different keywords and types shouldn't be mixed within a program like that. Maybe they are the same under the covers but that is irrelevant and those kinds of assumptions are generally bad. You know that.
Jun 22, 2009 at 7:36pm
Ok, so I was just making things unnecessarily complicated. Thanks again everyone.
Jun 22, 2009 at 7:37pm
It's not an assumption. It's a fact. A bool is an integral type.

IMO, all conversions to bool from the built-in types should be silent. I'm sick and tired of writing if (!!string_pointer). That has to be the most retarded thing I've seen.

Can you think of any possible circumstance when converting from bool to int would give problems? I've done all sorts of things with bools like a*(b<c), and I'm pretty sure that's how the conversion was intended to be used.
Jun 22, 2009 at 8:04pm
Well, not all people agree with you in all contexts. That's why the safe bool idiom was invented.

http://www.artima.com/cppsource/safebool.html

But I have to agree with kempofighter. C++ wants to be 100% typesafe, but it can't be because of its ties to C. Allowing implicit bool-int conversions is contrary to the type safety of the language.

Just ask Msoft, who had to add a compiler warning re: just that due to performance issues with MFC.

Jun 22, 2009 at 8:17pm
I agree with helios, and in fact this is one of the things that bugs me about Java.
Jun 22, 2009 at 8:29pm
I was simply stating that in the context of the original example the mixture of the bool and int variables didn't make logical sense. What is the point of unnecessarily mixing types and relying on implicit casting when you can just write clearer code to begin with? Have a nice day Helios.
Jun 22, 2009 at 8:42pm
I also agree with kempofighter XD
Topic archived. No new replies allowed.