Adam Joseph Looze
Adam Joseph Looze

Reputation: 1021

C++ Temperature Conversion

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

Answers (2)

Erik von Reis
Erik von Reis

Reputation: 11

Added to Drew Dormann's answer: it's (9.0f/5.0f) for the proper conversion from C to F.

Upvotes: -1

nico
nico

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

Related Questions