From e93c300144d199c5983ec98c9ec651b9a1b16523 Mon Sep 17 00:00:00 2001 From: ludwig Date: Tue, 27 Sep 2022 19:20:11 -0500 Subject: [PATCH] Update 'converter.cpp' Fixed some formatting, and playing with kelvin conversions --- converter.cpp | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/converter.cpp b/converter.cpp index 22ef8c6..d1e98c2 100644 --- a/converter.cpp +++ b/converter.cpp @@ -1,5 +1,5 @@ #include - +#include using namespace std; @@ -7,18 +7,28 @@ using namespace std; void Menu(){ // <- The menu is (mostly) static, no need for it to be in main cout << "Simple temperature converter \n"; - cout << "Conversion options \n [1]C to F \n [2]F to C \n [3]Quit" << endl; - cout << "Select option [1-3]: "; + cout << "Conversion options \n [1]C to F \n [2]F to C \n [3]F to K \n [4]C to K \n [5]Quit" << endl; + cout << "Select option [1-5]: "; } -double C, F; +double C, F, K; + +double convertC2K(double C, double K) { + K = (C + 273.15); + return(K); +} -double convertC(double C, double F){ // <- converts to Celsius +double convertF2K(double F, double K) { + K = (((F - 32) * 5/9 ) + 273.25); + return(K); +} + +double convertC(double C, double F) { // <- converts to Celsius C = (F - 32) / 1.8; return(C); } -double convertF(double C, double F){ // <- converts to fahrenheit +double convertF(double C, double F) { // <- converts to fahrenheit F = (C * 1.8) + 32; return(F); } @@ -28,21 +38,34 @@ int main() { Menu(); cin >> input; - while (input != 3) { + while (input != 5) { if (input == 1) { cout << "Enter temperature in Celsius: "; cin >> input; C = input; - cout << convertF(C, F) << endl; + cout << setprecision(2) << convertF(C, F) << endl; break; - } else if (input == 2) { + } + else if (input == 2) { cout << "Enter temperature in Fahrenheit: "; cin >> input; F = input; - cout << convertC(C, F); + cout << setprecision(2) << convertC(C, F) << endl; break; } + else if (input == 3) { + cout << "Enter temperature in Fahrenheit: "; + cin >> input; + F = input; + cout << setprecision(2) << convertF2K << endl; + } + else if (input == 4) { + cout << "Enter temperature in Celsius: "; + cin >> input; + C = input; + cout << setprecision(2) << convertC2K << endl; + } } cout << "\nExiting..."; return(0);