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.
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.
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.
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.
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.
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.
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.
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.
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.