Unable to Round Up

[Write your question here.]

I am working on a program to convert degrees Fahrenheit into degrees Celsius,
but am unable to round the result to the nearest integer when it needs to be
rounded up. I've tried several things, any advice would be helpful.
[Put the code you need help with here.]
#include <iostream>
using namespace std;
int main()
{
int F;
int C;
cout << "Please enter Fahrenheit degrees: ";
cin >> F;
C = (F - 32)/1.8;
cout << "Celsius: " << C << "\n";

return 0;
}
integer math will always truncate any fraction, 3.2 will be 3. 4.8 will be 4.

You should use floats/doubles instead of ints when decimals are important.

Consider using the <cmath> round() function to help getting integral values of floating point numbers.

http://www.cplusplus.com/reference/cmath/round/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
   double F;

   std::cout << "Please enter Fahrenheit degrees: ";
   std::cin >> F;
   std::cout << '\n';

   double C = (F - 32.0) / 1.8;

   std::cout << "Celsius: " << C << '\n';
}
I think the problem was I didn't use cout.setf(ios::fixed) along with cout.precision(0). But I will look into the <cmath> round() function. Thanks!
Using iostream flags really doesn't alter the stored decimal portion even if a floating point, it only changes how it is represented on output.
Topic archived. No new replies allowed.