# Help please with something pretty simple

• 09-16-2003
sweetly
Help please with something pretty simple
I'm trying to finish a homework problem that requires an integer as an input but the output result must be in decimal form.
Here's what I have...the program will run & works except for the decimal for the quotient.

[code]
int num1, num2;
float sum, difference, product, quotient;
sum = num1 + num2;
difference = num1 - num2;
product = num1 * num2;
quotient = num1 % num2;
cout << "This program computes the sum, difference, product, and quotient of any two integers" << endl;
cout << "enter an integer then press <Enter>: ";
cin >> num1;
cout << "enter your second integer then press <Enter>: ";
cin >> num2;

// Add num1 + num2 for the sum.
sum = num1 + num2;

// Subtract num2 from num1 for the difference.
difference = num1 - num2;

// Multiply num1 x num2 for the product.
product = num1 * num2;

// Divide num1 by num2 for the quotient.
quotient = num1 / num2;

// Display the sum of num1 + num2.
cout << "The sum of your two numbers is " << sum << endl;
cout << "The difference of your two numbers is " << difference << endl;
cout << "The product of your two numbers is " << product << endl;
cout << "The quotient of your two numbers is " << quotient << endl << endl;
return 0;
}
• 09-16-2003
twm
When you have two integers and the operation on them has to result in floating-point, cast one of the integers to double or float:
Code:

`quotient = static_cast<float>(num1) / num2;`
But not the whole operation or you won't get the precision you want. The result of an integer operation will be cast to float instead of the operation being performed as float.
• 09-16-2003
sweetly
Thank you, thank you, thank you, twm!!!

That works perfectly. No wonder I couldn't figure it out..we haven't covered <static_cast> in class yet. :eek:
• 09-16-2003
twm
>we haven't covered <static_cast> in class yet.
Well, you can do it like this too:
Code:

```// Old style cast quotient = (float)num1 / num2;```
• 09-16-2003
sweetly
That's what I was trying to accomplish by putting

int num1, num2;
float sum, difference, product, quotient;

but that didn't make it work.

I was mising the float in quotient = (float) num1 / num2;

Thanks again!
• 09-17-2003
...
(float)num is the C style of doing it.

C++ also supports float(num).