-1

I want to do convert Fahrenheit to Celsius and I use the formula I put cout.setf(ios::fixed); cout.precision(0); // because I want the result to be a whole number

float c;
float f;
c = 5.0/9.0*(f-32)

cout << "Enter Fahrenheit":
cin >> f;
cout << "Celsius"
     << c
      << endl;

Please, I need help is for my homework, thanks.

Mely
  • 11
  • 1
  • Probably not a duplicate, but should be at least helpful: https://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout – Tas Oct 03 '18 at 01:53
  • Also maybe https://stackoverflow.com/questions/17341870/correct-use-of-stdcout-precision-not-printing-trailing-zeros – Tas Oct 03 '18 at 01:53
  • You seem to be running your conversion **before** knowing what `f` is. Maybe try moving it underneath the `cin` line. – Shadow Oct 03 '18 at 03:50

1 Answers1

2

If you really want to truncate the value, use a cast:

#include <iostream>

int main()
{
    float f;
    std::cout << "Enter Fahrenheit: ";
    std::cin >> f;
    float c = 5.0f / 9.0f * (f - 32.f);
    std::cout << "Celsius: "
              << static_cast<int>(c)
              << '\n';
}

if you however want to round the value:

#include <iostream>
#include <cmath>

int main()
{
    float f;
    std::cout << "Enter Fahrenheit: ";
    std::cin >> f;
    float c = 5.0f / 9.0f * (f - 32.f);
    std::cout << "Celsius: "
              << std::round(c)
              << '\n';
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43