### Output problem

Hi, I'm having trouble getting the correct output.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include #include using namespace std; int degree; int power; double x; int Convert(int output); int main() { int output; char answer; double exponentiated; double exponent; do { cout << "Enter degree: \n"; cin >> degree; cout << "Enter power: \n"; cin >> power; cout << "Enter 'x': \n"; cin >> x; cout << "Output: "; cout << Convert(output); exponent = (degree - power); exponentiated = pow(x,exponent); cout << output * exponentiated << " times Y^" << power << endl; cout << "\nWould you like to try another? (y/n): "; cin >> answer; } while (answer == 'y' || answer == 'Y'); system("PAUSE"); return 0; } int Convert(int output) { long factorial1; factorial1 = 1L; for(long i=2L; i <= degree; i++) factorial1 *= i; long factorial2; factorial2 = 1L; for(long i=2L; i <=power; i++) factorial2 *= i; long factorial3; factorial3 = 1L; for(long i=2L; i <= (degree - power); i++) factorial3 *= i; return output = (factorial1) / ((factorial2) * (factorial3)); }``````

The problem seems to be here:

 ``12`` ``````exponentiated = pow(x,exponent); cout << output * exponentiated << " times Y^" << power << endl;``````

Whenever I exclude the output * exponentiated and just put

 `` `` ``cout << output << " times" << exponentiated ``

It works. But I want the program to do the calculations itself.

Edit:

I think I fixed the problem. I moved output * exponentiated to the Convert() function.
 ``1234`` `````` exponent = (degree - power); exponentiated = pow(x,exponent); output = (factorial1) / ((factorial2) * (factorial3)) * exponentiated;``````

Last edited on
closed account (zb0S216C)
You need to enclose the sum in a set of parentheses:

 `` `` ``std::cout << ( output * exponentiated ) << ...;``

Wazzak
I've fixed the problem which I think was because I had to use Convert(output) instead of just output.

Putting it in a set of parenthesis didn't work :/
I was still getting answers such as 210.4049302e+10X^3.

When the answer should have been 540X^3.

Last edited on
Topic archived. No new replies allowed.