Very Basic Temp Convert Program Would Appreciate Some Eyes

First of all, I have only been programming
for a couple of weeks for a college class.
I thank you in advance for any insight.

My problem is when I convert Fahrenheit to Kelvin, I get an 7 digit number with no decimals.

//Write a program that converts Fahrenheit to Kelvin.
//Round to one decimal place. The formula is as follows:
//(F - 32) * (5 / 9) + 273.15

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

int main ()

int tempK, tempF, input;

tempK = (tempF) - 32 * (5 / 9) + 273.15;

cout<< "== Fahrenheit to Kelvin converter =="<< endl;
cout <<"Enter Fahrenheit:"<< endl; //prompt user

cin>> input;

cout<< "Kelvin: " << setprecision(2) << tempK<< showpoint;

return 0;


I know this is a very basic program, but I am making progress.
Thank you.
You did not initialize variable tempF. So the result is undefined.
The numeric int type is unsuitable here. Floating point values should be of type double or type float.
Hello mizztrixi333, my first question would be what program are you using?

Another problem would be your variable types. If anyone else stumbles across this question, correct me if I'm wrong but, when decimals are involved, the variable type (int), should be float instead. It would be optimal to initialize those variables to 0, to prevent junk values. Also there's no need to use input. Just replace cin >> input; with cin >> tempF; tempF is the temperature you're being asked to input.

2 way of doing so would be:
float tempF = 0;
float tempK;

there's no need to assign value of zero to tempK since the forumla will be assigned to it.

There's something weird about your formula. The forumla you used in your code was incorrect for some reason, as I kept getting a bad value. I tried the formula in your code and the formula on the web (pretty much the same) for assurance, and I had no luck. I tried this forumla: tempK = ((tempF - 32) * 5) / 9 + 273.15; and had success, and if you look closely, you can see what has changed.

tempK = ((tempF - 32) * 5) / 9 + 273.15;

I guess this has something to do with how programming handles mathemiatical equations or something, but I've never had this problem with operations (hadn't tried temp converters before this)

This is what I ended up with:

#include <iostream>
#include <iomanip>

using namespace std;

int main ()

float tempK, tempF = 0;

cout << "== Fahrenheit to Kelvin converter ==" << endl;

cout <<"Enter Fahrenheit: "; //prompt user| edit: endl deletion is optional. allows user to enter the value on the same line as statement

tempK = ((tempF - 32) * 5) / 9 + 273.15; // good idea to put this here(after the user inputs the value) so nothing goes wrong

cout << fixed; // put's in decimal form or else you will get scientific notation

cout << "Kelvin: " << setprecision(2) << tempK << showpoint;

cin.get(); // makes the user press enter before program closes, optional

return 0;


I believe this should be it.
Last edited on
@ranfan you are on roughly the right lines, but may not have understood exactly why.

The original formula posted had two main errors, one being operator precedence (multiplication and division are done before addition and subtraction) and the other was the use of integer division which will give an integer result (i.e. decimal places are truncated/discarded).

The comment describing the purpose of fixed is not accurate.

The purpose and example usage of showpoint:
Last edited on
Thank you Everyone for taking the time to help me.

Excellent advice! I see where I went wrong.

After making a few changes I am still so close.

Now, my program outputs the same number after the temp conversion for every different test number inputted.

When using the formula the teacher gave me (F - 32) * (5 / 9) + 273.15
I get a different result. For example inputting 52.2 results 273.15 (which still repeats for every different number inputted)

With the new formula I get 52.2 results 255.37

order of operations? - as previously suggested
Am I correct in using both fixed and showpoint ?

I am using Code Blocks.

This is my updated program.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()

double tempK, tempF = 0;

cout<< "== Fahrenheit to Kelvin converter =="<< endl;

cout <<"Enter Fahrenheit:"<< endl; //prompt user

tempK = ((tempF - 32) * 5) / 9 + 273.15;

cout<< fixed;

cin>> tempF;

cout<< "Kelvin: " << setprecision(2) << tempK << showpoint;

return 0;


Thanks again!
This formula is mathematically correct: (F - 32) * (5 / 9) + 273.15
But when the C++ compiler translates it into code, this part (5 / 9) is treated as an integer division.

Try this:
cout << "(5/9) = " << (5/9) << endl;
(5/9) = 0

Yes, you read that correctly. 5 divided by 9 gives zero.
The reason is both 5 and 9 are integers, so an integer division is performed. The whole-number part of the answer is kept, and any part after the decimal point is discarded.

If you look back to the formula (F - 32) * (5 / 9) + 273.15 you can see what is happening now. It simplifies to this:
(F - 32) * 0 + 273.15
Since anything multiplied by zero results in zero, the entire left-hand expression disappears and we are left with just 273.15

A correct way to code this is to make either one or both of 5 and 9 into floating point values. Try this:
cout << "(5.0/9.0) = " << (5.0/9.0) << endl;
(5.0/9.0) = 0.555556

When a literal value such as 5.0 is used, the compiler treats it as type double and carries out the floating-point division as required.

So that's the explanation as to why the mathematically correct formula gave unexpected results.

And finally, this is how to correct it: (F - 32) * (5.0 / 9.0) + 273.15
Last edited on
Am I correct in using both fixed and showpoint ?

If you use showpoint like this:
cout<< "Kelvin: " << setprecision(2) << tempK << showpoint;
then it has no effect whatsoever.

It should be placed before the value to be output, like this:
cout << "Kelvin: " << showpoint << setprecision(2) << tempK;

However, when using fixed, then showpoint isn't really needed.


Thank you for the insight Chervil. After adding .0 to each integer

and moving the formula and the manipulator to read under cin>>, It passed flawlessly.

Again a million thanks!
Topic archived. No new replies allowed.