Skip to content

Instantly share code, notes, and snippets.

@jandk
Created December 26, 2023 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jandk/e8802d64a754eb64bcde4199d75f087b to your computer and use it in GitHub Desktop.
Save jandk/e8802d64a754eb64bcde4199d75f087b to your computer and use it in GitHub Desktop.
#include <iostream>
double toKelvin(double value, char unit) {
switch (srcUnit) {
case 'C':
return num + 273.16;
case 'F':
return ((num - 32) / 1.8) + 273.16;
case 'K':
return num;
case 'R':
return ((num - 491.67) / 1.8) + 273.16;
default:
std::cout << "Please enter a valid conversion unit!";
}
}
double fromKelvin(double value, char unit) {
switch (dstUnit) {
case 'C':
return num - 273.16;
case 'F':
return (1.8 * (num - 273.16)) + 32;
case 'K':
return num;
case 'R':
return (1.8 * (num - 273.16)) + 491.67;
default:
std::cout << "Please enter a valid conversion unit!";
}
}
int main() {
double num;
char srcUnit;
char dstUnit;
std::cout << "Please enter a number: ";
std::cin >> num;
std::cout << "Please enter the source srcUnit: ";
std::cin >> srcUnit;
std::cout << "Please enter the destination srcUnit: ";
std::cin >> dstUnit;
double kelvin = toKelvin(num, srcUnit);
double result = fromKelvin(kelvin, dstUnit);
std::cout << "Result: " << result << " " << dstUnit;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment