# C++ Calculator: Temperature conversion Formulas Not Working!

So, I’ve been trying to code a temperature converter into my calculator
The pseudo-code is something like this:

Take User Input:
If input is C (Celsius) convert into Fahrenheit and Kelvin, Display Results.
If input is K (Kelvin) convert into Fahrenheit and Celsius, Display Results.
If input is F (Fahrenheit) convert into Celsius and Kelvin, Display Results.

Works fine, input is taken properly and displayed. BUT when I try to convert the temperature: For example: 20.5 Degrees Celsius you’d think it would output:

68.9 Fahrenheit and 293.65 Kelvin right?
NOPE the code outputs some ridiculous number that is NOWHERE NEAR correct.

these are the formulas I’m using: (If they ARE incorrect please tell me! )

1. (Celsius to Kelvin): C + 273.15 = K,

2. (Kelvin to Celsius): K - 273.15 = C,

3. (Celsius to Fahrenheit): (C * 9/5) + 32 = F,

4. (Fahrenheit to Celsius): (F - 32) * 5/9 = C,

5. (Kelvin to Fahrenheit): (K - 273.15) * 9/5 + 32 = F,

6. (Fahrenheit to Kelvin): (F - 32) * 5/9 + 273.15 = K,

And here is the code(Please Know: This is not the calculators full code if you want to see it follow this link C++ Calculator: One more thing: The project can’t take user input because Bash is not available in Projects(If it is please tell me how to get it!) I recommend copy and pasting it into the C++ courses like:
Compile and Execute)

``````char temp_input;
float temp_x;
float temp_k;
float temp_c;
float temp_f = 9 / 5;
float temp_f1;
float temp_f2;
float temp_f3;
float temp_f4 = 5 / 9;

std::cout << "Welcome to our useful temperature converter!\n";
std::cout << "Please input the unit of measurement for your temperature (K = Kelvin, F = Fahrenheit, C = Celsius)\n";
std::cout << ">";
std::cin >> temp_input;
std::cout << "\n";

if(temp_input == 'K') {
std::cout << "Please input the temperature in Kelvin.\n";
std::cout << ">";
std::cin >> temp_x;
std::cout << "\n";

temp_c = temp_x - 273.15;

temp_f1 = temp_x - 273.15;
temp_f2 = temp_f1 * temp_f;
temp_f3 = temp_f2 + 32;

std::cout << "Your Input: Kelvin: " << temp_x << "\n";
std::cout << "Celsius: " << temp_c << "\n";
std::cout << "Fahrenheit: " << temp_f3 << "\n";
}

else if(temp_input == 'C') {
std::cout << "Please input the temperature in Celsius.\n";
std::cout << ">";
std::cin >> temp_x;
std::cout << "\n";

temp_k = temp_x + 273.15;

temp_f1 = temp_x / temp_f;
temp_f2 = temp_f + 32;

std::cout << "Your Input: Celsius: " << temp_x << "\n";
std::cout << "Kelvin: " << temp_k << "\n";
std::cout << "Fahrenheit: " << temp_f2 << "\n";

}

else if(temp_input == 'F') {
std::cout << "Please input the temperature in Fahrenheit.\n";
std::cout << ">";
std::cin >> temp_x;
std::cout << "\n";

temp_f1 = temp_x - 32;
temp_f2 = temp_f1 * temp_f4;
temp_k = temp_f2 + 273.15;

temp_c = temp_f1 * temp_f;

std::cout << "Your Input: Fahrenheit: " << temp_x << "\n";
std::cout << "Kelvin: " << temp_k << "\n";
std::cout << "Celsius: " << temp_c <<  "\n";
}

``````

Here a few issues to correct:

• Issue #1. Programming languages can differ in how they implement rules for operations. In C++, if you divide two integers, you get integer division with the result also being an integer.
For example,
``````std::cout << 9 / 5 << "\n";
// 1
std::cout << 5 / 9 << "\n";
// 0
std::cout << 2 / 4 << "\n";
// 0
std::cout << 23 / 5 << "\n";
// 4
``````

Even though you have made the declarations,

``````float temp_f = 9 / 5;
// 1
float temp_f4 = 5 / 9;
// 0
``````

what will happen is that first integer division will be carried out which will result in an integer (with the fractional part thrown away). Then this integer will be assigned to the `float` variable(s).
You can rectify this by converting/casting one or both of the integers into floats/doubles before division.

Any of these will work:

``````float temp_f = 9.0 / 5;
float temp_f = 9 / 5.0;
float temp_f = 9.0 / 5.0;
float temp_f = float(9) / 5;
float temp_f = 9 / float(5);
float temp_f = float(9) / float(5);

// All will evaluate to 1.8
``````
• Issue #2. In the `else if(temp_input == 'C')` block, you wrote:
``````//
// You wrote:
temp_f1 = temp_x / temp_f;
temp_f2 = temp_f + 32;

//
// It should be:
temp_f1 = temp_x * temp_f;
temp_f2 = temp_f1 + 32;
``````
• Issue #3. In the `else if(temp_input == 'F')` block, you wrote:
``````// You wrote:
temp_c = temp_f1 * temp_f;

// It should be:
temp_c = temp_f1 * temp_f4;
``````

You can have a look at the Temperature Conversion Formulas.

1 Like

Thanks! That solved the problem! It now runs perfectly And now that’s two of my problems you’ve solved!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.