'TRUE' was not declared in this scope C++ - c++

So I have this simple function and it is suppose to return true or false. True is only returned 2% of the time and false every other time. However, whenever I try to compile the full code I get the error message;
'TRUE' was not declared in this scope C++
I am aware I can use int instead of bool and just use 1 for true and 0 for false, but I want to figure out why this won't work. Any help?
Here is my function:
bool Bunny::determineMutant()
{
int rand_num = rand() % 100 + 1; //random num 1-100
if(rand_num == 1 || rand_num == 2) return TRUE;
else return FALSE;
}

TRUE and FALSE are often associated with the Win32 type BOOL, alternatively a macro for the integers 1 and 0. The actual error is there since the definition of TRUE is not available.
For the built in C++ type bool use true and false. I think this should be the preferred solution here.
As a side note, the code could be simplified to return based on the condition in the if() which is already a bool;
return (rand_num == 1 || rand_num == 2);

The keywords in C++ are lowercase true and false.
The all-caps TRUE and FALSE are generally preprocessor macros of 1 and 0 respectively in VC++, and are used to return the type BOOL.

Related

C++ syntax when initializing the instance variables

What does it mean in C++
int x;
x = GetMethod("OpponentCalledOnTurn") == 1;
Note : why is there a "==1" part.
I am Novice to c++.
This will set x to 1 if GetMethod("OpponentCalledOnTurn") == 1 evaluates to true and to 0 if it evaluates to false.
The underlying rule here: A boolean value can be converted to other integer types, that will result in 1 for true and 0 for false.
== is the equality comparison operator.
So GetMethod("OpponentCalledOnTurn") == 1 first calls the function GetMethod, passing the given string literal as an argument. The return value of that function call is then compared to 1. That comparison evaluates to either true if the return value equals 1, or false otherwise.
x = then assigns that true or false to x. Since x is of type int and not bool (the type of true and false), true is converted to 1 and false is converted to 0.
Effectively, if GetMethod("OpponentCalledOnTurn") returns 1, x will end up equaling 1, otherwise it will end up equaling 0.

Integer to Boolean strange syntax [duplicate]

This question already has an answer here:
The Definitive C++ Book Guide and List
(1 answer)
Closed 7 years ago.
I'm less than a year into C++ development (focused on other languages prior to this) and I'm looking at a guy's code who's been doing this for two decades. I've never seen this syntax before and hopefully someone can be of some help.
bool b; // There exists a Boolean variable.
int i; // There exists an integer variable.
sscanf(value, "%d", &i); // The int is assigned from a scan.
b = (i != 0); // I have never seen this syntax before.
I get that the boolean is being assigned from the int that was just scanned, but I don't get the (* != 0) aspects of what's going on. Could someone explain why this person who knows the language much better than I is doing syntax like this?
Have a read here:
http://en.cppreference.com/w/cpp/language/operator_comparison
The result of operator != is a bool. So the person is saying "compare the value in i with 0". If 'i' is not equal to 0, then the '!=' returns true.
So in effect the value in b is "true if 'i' is anything but zero"
EDIT: In response to the OP's comment on this, yes you could have a similar situation if you used any other operator which returns bool. Of course when used with an int type, the != means negative numbers evaluate to true. If > 0 were used then both 0 and negative numbers would evaluate to false.
The expression (i != 0) evaluates to a boolean value, true if the expression is true (i.e. if i is non-zero) and false otherwise.
This value is then assigned to b.
You'd get the same result from b = i;, if you prefer brevity to explicitness, due to the standard boolean conversion from numeric types which gives false for zero and true for non-zero.
Or b = (i != 0) ? true : false; if you like extraneous verbosity.
(i != 0) is an expression that evaluates to true or false. Hence, b gets the value of true/false depending on the value of i.
This is fairly fundamental syntax. The != operator performs a "not equal to" comparison.
You may be being confused by the shorthand of initialising a bool directly from the result of a comparison operator, but the syntax itself is not esoteric.
The program is essentially equivalent to:
bool b;
int i;
sscanf(value, "%d", &i);
if (i != 0)
b = true;
else
b = false;
The key is that i != 0 is itself an expression that evaluates to true or false, not some magic that may only be used in an if statement.
Basically, if the condition (i not_equal_to 0 ) is satisfied, b gets the value "true". Else b gets the value "false".
Here, "i != 0" is a boolean expression that will be true if "i" is non-zero and false if it is zero.
All that is happening here is the result of that expression is being assigned to a variable.
You could also do things like...
boolean canDrinkAlcohol = (person.age() >= 18 && person.country.equals("UK") || person.age() >= 21 && person.county.equals("US"));
...
if(canDrinkAlcohol) {
...
}
or something

Comparison of int values in an if statement

I had a question in my test paper in which we had to compare the values of int type variables. The first thought that came to my mind was that it was missing the && operator but i am not sure.
int a=2, b=2, c=2;
if(a==b==c)
{
printf("hello");
}
I have a doubt, will the above statement will execute or not in c or c++? Can i have the reason as well.
Thank You
It will execute but with what I believe unexpected results to you.
One of the == will evaluate to a boolean value, which will then be converted to an int and then the second comparison will be performed, comparing an int to either 1 or 0.
The correct statement is a==b && b==c.
For example:
3 == 3 == 3
evaluates to
true == 3
1 == 3
false
a==b==c
is equivalent to
(a == b) == c
The result of a == b is 1 (if true) or 0 (if false), so it will probably not achieve what you expect.
Use a == b && b == c to check if the value of the three objects are equal.
a == b == c is a comparison between c and result of a==b (1 or 0) operation.
use a==b&&b==c.
the condition a==b==c is equivalent to (a==b)==c which will provide the required result iff c==1, else the code will fail.

In C++ syntax, can the condition of the if else, return a int and still execute the statements within

Here is the code which compiles :
int select_object = 0;
if( select_object ) //condition returns an int
{
printf("Hello");
}
if condition returns an int and not a boolean will the hello be printed ? When I tested this it printed hello.
Any idea why even for an int it executes the print statement.
THanks
In C and C++, any nonzero integer or pointer is considered true. So, since select_object is 0, it should not be printing Hello.
Boolean logic
1 = True
0 = False
1 && 0 = False 0
1 && 1 = True 1
1 || 1 = True 1
1 || 0 = True 1
So the answer is for non-zero it is considered true, for 0 it is considered false. If your value (your int) returns 0 it won't execute. If it returns a value that is not 0 it will execute.
In C or C++, a bool is just a fancy way of saying 'int with special values'. Every logical test (if, while, for, etc) can use an int or a pointer for its test instead of a bool, and anything that isn't 0 is true. NULLs and 0 are equal in this sense.

isalpha(<mychar>) == true evaluates to false?

string temp is equal to "ZERO:\t.WORD\t1" from my debugger. (the first line of my file)
string temp = RemoveWhiteSpace(data);
int i = 0;
if ( temp.length() > 0 && isalpha(temp[0]) )
cout << "without true worked" << endl;
if ( temp.length() > 0 && isalpha(temp[0]) == true )
cout << "with true worked" << endl;
This is my code to check if first character of temp is a a-z,A-Z. The first if statement will evaluate to true and the 2nd to false. WHY?!?!?! I have tried this even without the "temp.length() > 0 &&" and it still evaluates false. It just hates the "== true". The only thing I can think of is that isalpha() returns != 0 and true == 1. Then, you could get isalpha() == 2 != 1. But, I have no idea if C++ is that ... weird.
BTW, I dont need to know that the "== true" is logically pointless. I know.
output was
without true worked
Compiled with CodeBlock using GNU GCC on Ubuntu 9.10 (if this matters any)
The is* functions are only guaranteed to return a non-zero value if true, NOT necessarily a 1. A typical implementation is table based, with one entry in the table for each character value, and a set of bits defining which bit means what. The is* function will just AND the right bitmask with the table value, and return that, which will only be the value 1 for whichever type happens to have been given bit position 0.
E.g.:
#define __digit 1
#define __lower 2
#define __upper 4
extern int __type_table[];
int isdigit(int c) {
return __type_table[c+1] & __digit;
}
int isalpha(int c) {
return __type_table[c+1] & (__lower | __upper);
}
int islower(int c) {
return __type_table[c+1] & __lower;
}
int isupper(int c) {
return __type_table[c+1] & __upper;
}
Where __type_table is defined as something like int __type_table[UINT_MAX+1]; and would be initialized so (for example) __type_table['0'+1] == __digit and __type_table['A'+1] == __upper.
In case you care, the '+1' part is to leave a spot at the beginning of the table for EOF (which is typically defined as -1).
isalpha doesn't return true, it returns non-zero. This is quite common for API designed for C.
Note that in the expression isalpha(ch) == true, the subexpression true is promoted to type int with value 1.
Well, the documentation suggests that it returns either zero or non-zero, not necessarily just false or true. So you'd better check (isalpha(temp[0]) != 0).
Do not isalpha(ch) == true, but !!isalpha(ch) == true