Thread: An Easy Question about Return 0 or 1

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    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. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very Easy Question
    By Paul22000 in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2008, 03:15 PM
  2. easy question for some.. however
    By webznz in forum C Programming
    Replies: 5
    Last Post: 10-24-2007, 02:34 AM
  3. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  4. easy question again
    By lessrain in forum C Programming
    Replies: 13
    Last Post: 07-04-2002, 09:07 AM
  5. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM