3

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;
}
Niall
  • 30,036
  • 10
  • 99
  • 142
David
  • 103
  • 1
  • 1
  • 8

2 Answers2

6

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);
Community
  • 1
  • 1
Niall
  • 30,036
  • 10
  • 99
  • 142
4

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.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218