Thread: value...?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    value...?

    I'm a little confused.
    in this program ive writted from a book it says

    it sets a variable Value and says, if number > 20, set Value = 1 else Value = 0 right. if its 1, display an error message otherwise continue with program.

    is Value a default variable built for this specific task?

    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef unsigned short int USHORT;
    
    short Factor(USHORT, USHORT*, USHORT*);
    
    int main()
    {
    	USHORT Number, Squared, Cubed;
    	short Error;
    
    	cout <<"Enter number (0-20): ";
    	cin >> Number;
    
    	Error = Factor(Number, &Squared, &Cubed);
    
    	if(!Error)
    	{
    		cout <<"Number: " << Number << "\n";
    		cout <<"Square: " << Squared << "\n";
    		cout <<"Cube: " << Cubed << "\n";
    	}
    	else
    		cout <<"Error Encountered!!!\n";
    	system("PAUSE");
    	return 0;
    }
    
    short Factor(USHORT n, USHORT *pSquared, USHORT *pCubed)
    {
    	short Value = 0;
    	if(n > 20)
    		Value = 1;
    	else
    	{
    		*pSquared = n*n;
    		*pCubed = n*n*n;
    		Value = 0;
    	}
    	return Value;
    }
    i am just wondering. if it isnt built for this, can someone just explain how value is making the decision to display error or not because its not in the main function of the program and it isnt set to any other variable or whatever. im a little confused.

    Im just confused as to how Value knows to display ERROR

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The error message is in main. The check is made in the function Factor and 0 or 1 is returned by this function. This value is assigned to Error which is checked to produce the message or not. It's all in the code you've got there.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    oh ok thanks alot mate.

    i get it, i was a bit confused with it.

    thanks. hugo.

Popular pages Recent additions subscribe to a feed