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! )
-
(Celsius to Kelvin): C + 273.15 = K,
-
(Kelvin to Celsius): K - 273.15 = C,
-
(Celsius to Fahrenheit): (C * 9/5) + 32 = F,
-
(Fahrenheit to Celsius): (F - 32) * 5/9 = C,
-
(Kelvin to Fahrenheit): (K - 273.15) * 9/5 + 32 = F,
-
(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";
}
PLEASE help me figure this out! I can’t (at the moment) And it’s driving me insane!