1. ## An Easy Question about Return 0 or 1

Hi everybody, new to the forums. I'm relatively new to programming, and I'm trying to teach myself as a hobby. I'm using Teach Yourself C++ in 21 Days, and I'm currently on pointers and references.

I'm struggling with this code. I feel like I understand the result, but I don't understand why it does what it does . . .

Code:
```#include <iostream>

using namespace std;

short Factor (int n, int* pSquared, int* pCubed);

int main()
{
int number, squared, cubed;
short error;
cout << "Enter a number (0 - 20): ";
cin >> number;

error = Factor(number, &squared, &cubed);
cout << "Error: " << error;

if (!error)
{
cout << "number: " << number << endl;
cout << "square: " << squared << endl;
cout << "cubed: " << cubed << endl;
}
else
cout << "Error encountered!!" << endl;  */
return 0;
}

short Factor(int n, int *pSquared, int *pCubed)
{
short Value = 0;
if (n > 20)
Value = 1;
else
{
*pSquared = n*n;
*pCubed = n*n*n;
Value = 0;
}
return Value;
}```
(This code is from page 269, "SAMS Teach Yourself C++ in 21 Days by Liberty and Jones)
What's confusing me is the if statement !error. I read this as "if not 1" if the user input is greater than 20, or "not 0" if it is less than 20. But the function returns either 1 or 0 as the value for error, so either way the result is "not error." Shouldn't the else statement run anyway? I know 0 means the function ran as intended. But it seems to me the if statement should be "error != 1". I'm sure this is an obvious thing, but I'm definitely missing something. Thanks for any help!

2. In C the value of 0 is considered false; all other integer values are considered true.

the value of expression "!0" is 1
the value of the expression "!x" is 1 for x equal 0 and the expression is 0 for all other values of x.

Code:
```if (1) {
/* statement is always true */
}
if (0) {
/* statement is always false */
}```
Tim S.

3. The book is misusing a short as a boolean value. As this is C++ not C, there is no reason to do so. "if" evaluates it's condition by checking for 0. 0 is considered "false" while anything else (including 1) is considered "true". The book mixes C and C++. I simplified your code a bit using a bool variable instead of that short:

Code:
```#include <iostream>

using namespace std;

bool Factor( int n, int* pSquared, int* pCubed );

int main()
{
int number;
int squared;
int cubed;
bool success;

cout << "Enter a number (0 - 20): ";
cin >> number;

success= Factor( number, &squared, &cubed );

if( !success )
{
cout << "Error encountered!!" << endl;
}
else
{
cout << "number: " << number << endl;
cout << "square: " << squared << endl;
cout << "cubed: " << cubed << endl;
}

return 0;
}

bool Factor(int n, int* pSquared, int* pCubed)
{
if( n > 20 ) return false;

*pSquared = n*n;
*pCubed = n*n*n;

return true;
}```

4. Thanks for the replies. Both of these answers help a lot. It seems to be a poor way of setting up this code. It seems to me that there are many ways you could set up this code, and the book used one of the least optimal options!