hi, I was hoping someone could help me. I've been trying to do this for the past hour with no luck.

basically i'm trying to make a Celcius to Fahrenheit converter. So far, the converter works fine, except for the fact that I cant get it to display 2 decimal places when I get the conversion answer...

I've tried looking around everywhere on multiple sites and guides for precision setting, but they dont work...

what am I doing wrong? Please help...

// mytemp.cpp

#include <iostream>
using namespace std;

int main ()
{
  int c;
  cout << "Celcius: ";

  cin >> c;

  cout.setf(ios::fixed);
  cout << "Fahrenheit: " << ((c*9)/5)+32 << '\n';

  return 0;

}

Recommended Answers

All 3 Replies

setprecision(2)

#include <iostream>
#include <iomanip>
using namespace std;


int main ()
{
  int c;
  cout << "Celcius: ";

  cin >> c;

  cout.setf(ios::fixed);
  cout << "Fahrenheit: " << setprecision(2) << (((float)c*9.0)/5.0)+32.0 << '\n';

  return 0;

}

wow, thanks Ancient Dragon. that works great! i guess I missed the "float". I used just the setprecision but it didnt work

yup, without the float typecast and adding .0 to the numeric constants all you get is integer math, which drops all decimals.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.