hello-
i am trying to test two different answers with this function i am using. i am wanting to always compute the sum and product, and only the quotient if my int two is not a 0.
#include <iostream>
usingnamespace std;
bool compute(int one, int two, int &sum, int &product, double "ient);
int main()
{
int a = 11;
int b = 23;
int x;
int y;
double z;
compute(a, b, x, y, z);
cout << "The sum is " << x << endl;
cout << "The product is " << y << endl;
cout << "The quotient is " << z << endl;
return 0;
}
bool compute(int one, int two, int &sum, int &product, double "ient)
{
sum = one + two;
product = one * two;
if (two != 0)
{
quotient = static_cast<double>(one)/static_cast<double>(two);
returntrue;
}
else
{
cout << "The quotient cannot compute " << endl;
returnfalse;
}
}
/*
The sum is 34
The product is 253
The quotient is 0.478261
Press any key to continue . . .
The quotient cannot compute
The sum is 11
The product is 0
The quotient is -9.25596e+061
Press any key to continue . . .
*/
the first set of results is when i have int b set to 23. it seems to look fine, but when i change int b to 0 to try and get the 'else' to return it seems to still go through and compute the quotient. i'm not sure if i have the function itself wrong or if it has to do with what i'm returning in my main. if someone could point me in the right direction that would be great. thanks.
thank you. i think i see what you mean now. is there a way to set quotient to that cout in the else, or do i go about it by making a new variable in the function?
You wrote the function that return false if an argument of the second parameter is equal to zero. Why are trying to output quotient if you did not calculate it?!!! What is the problem?
You should write in main
1 2 3 4
bool result = compute(a, b, x, y, z);
cout << "The sum is " << x << endl;
cout << "The product is " << y << endl;
if ( result ) cout << "The quotient is " << z << endl;