am confused with the values and the way that error encountered when n>20

//how the error reported when n>20?
//what the values indicate? value 1 &value 0.


#include<iostream>
using namespace std;
short factor(int n,int *square,int *cubed);
int main()
{
int number,squared,cubed;
short error;
cout<<"enter anumber (0-20)"<<endl;
cin>>number;
error =factor(number,&squared,&cubed);//my 1st question is here
if(!error)
{
cout<<"nymber "<<number<<endl;
cout<<"squared "<<squared<<endl;
cout<<"cubed "<<cubed<<endl;
}
else
{cout<<"error encounterd\n"
;}
return 0;
}
short factor(int num,int *psquare,int *pcubed)
{
short value=0;
if(num>21)
value=1;//what this value indicates
else
{
*psquare=num*num;
*pcubed=num*num*num;
value=0;//and also here.
}
return value;/*how these two values are returned and in abook says bypass return values of the function which can then be reserved for reporting errors, what does it mean?*/
}
Hello Mehari belay,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

And do not be afraid to use blank lines to break up your code. It makes it easier to read as yu can see.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>

using namespace std;

short factor(int n, int *square, int *cubed);

int main()
{
	int number, squared, cubed;
	short error;

	cout << "enter anumber (0-20)" << endl;
	cin >> number;

	error = factor(number, &squared, &cubed);//my 1st question is here

	if (!error)
	{
		cout << "nymber " << number << endl;
		cout << "squared " << squared << endl;
		cout << "cubed " << cubed << endl;
	}
	else
	{
		cout << "error encounterd\n"; // <--- semi-colon should be here.
			//;  // <--- This works, but does not look good and could be confusing.
	}
	return 0;
}

short factor(int num, int *psquare, int *pcubed)
{
	short value = 0;
	if (num>21)
		value = 1;//what this value indicates
	else
	{
		*psquare = num*num;
		*pcubed = num*num*num;
		value = 0;//and also here.
	}
	return value;/*how these two values are returned and in abook says bypass return values of the function which can then be reserved for reporting errors, what does it mean?*/
}


Line 15: the the "&squared" and "&cubed" is sending the address of the variable to the function. Then in the function lines 38 and 39 dereference the pointer to store a new number. A very similar result as passing those variables by reference.

In the factor function "value" is being used like a "bool" to be used in main. It returns 0 or (false) or 1(true). Line 17 uses this value to either print the calculations or an error message.

Hope that helps,

Andy
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/


if ( ! error )
When is that condition true? When is it false?

We do know that the short error will have either value 1 or value .

There is an implicit conversion from integral value into bool value. The 0 converts into false, and everything else converts into true.
It is a slightly strange program design.

The code inside main() might have been written like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int number, squared, cubed;
    
    cout << "enter anumber (0-20)" << endl;
    cin >> number;

    if (number < 0 || number > 20)
    {
        cout << "error encountered\n";    
    }
    else 
    {    
        factor(number,&squared,&cubed);
        cout << "nymber "  << number << endl;
        cout << "squared " << squared << endl;
        cout << "cubed "   << cubed<<endl;
    }


Instead, in the original code the check whether the user input was in the specified range is made inside the function factor(). The return value (variable short error) is used to communicate back to the calling code whether or not the function succeeded. Often in C++ we would use a bool variable for this.

Next, the function needs to return more than one value. It is done here by passing a pointer to each of the two variables squared and cubed. The lines
1
2
        *psquare = num*num;
        *pcubed  = num*num*num;
are dereferencing the pointer, so that the result of the calculation num*num is stored in the original variable which was defined in main().

Usually in C++ we would prefer to pass by reference rather than by pointer in this sort of case.
Topic archived. No new replies allowed.