Reputation: 1021
I know this is very simple. But I am new to C++. I need to write a program that converts Centigrade temperatures to Fahrenheit and displays it. The formula provided by my professor is: Fahrenheit = (5/9) * Centigrade + 32.
#include <iostream>
using namespace std;
int main(){
float ctemp;
cout << "What is the temperature out in Centigrade? ";
cin >> ctemp;
float ftemp = (5/9) * ctemp + 32;
cout << "That is " << ftemp << " in Fahrenheit" << endl;
cin.get();
cin.get();
return 0;
}
No matter what temperature i enter for cin ctemp, i get 32. Something is wrong with my order of operations? Any help appreciated. I know this most likely simple, so please dont bag on this question.
Upvotes: 1
Views: 593
Reputation: 11
Added to Drew Dormann's answer: it's (9.0f/5.0f)
for the proper conversion from C
to F
.
Upvotes: -1
Reputation: 2121
float ftemp = (5/9) * ctemp + 32;
This line contains a minor bug. You should cast the ints to floats since that is the type you want to get. Something like this should do the trick:
float ftemp = (5.0f/9.0f) * ctemp + 32.0f;
Upvotes: 7