#include #include #include #include using namespace std; // Global variables double celsius; double kelvin; double fahrenheit; // Class encapsulates temperature converter class Temperature { public: void converter(char choise); void showmenu(); bool isvalid(char ch); }; // Display the converter void Temperature::converter(char choise) { switch(choise) { case '1': cout << "Enter the temperature in Celsius: "; cin >> celsius; fahrenheit = (celsius * 1,8) + 32; cout << celsius << " Celsius = " << fahrenheit << " Fahrenheit.\n\n"; break; case '2': cout << "Enter the temperature in Fahrenheit: "; cin >> fahrenheit; celsius = (fahrenheit - 32) * (5 / 9); cout << fahrenheit << " Fahrenheit = " << celsius << " Celsius.\n\n"; break; case '3': cout << "Enter temperature in Kelvin: "; cin >> kelvin; fahrenheit = (kelvin * 1,8) - 459,67; cout << kelvin << " Kelvin = " << fahrenheit << " Fahrenheit.\n\n"; break; case '4': cout << "Enter temperature in Fahrenheit: "; cin >> fahrenheit; kelvin = (fahrenheit + 459,67) * (5 / 9); cout << fahrenheit << " Fahrenheit = " << kelvin << " Kelvin.\n\n"; break; case '5': cout << "Enter temperature in Celsius: "; cin >> celsius; kelvin = celsius - 273,3; cout << celsius << " Celsius = " << kelvin << " Kelvin.\n\n"; break; case '6': cout << "Enter temperature in Kelvin: "; cin >> kelvin; celsius = kelvin + 273,3; cout << kelvin << " Kelvin - " << celsius << " Celsius.\n\n";
break; }; // End of switch }; //End of converter part void Temperature::showmenu() { cout << "This program can be used to convert temperatures. "; cout << "Choose one of the following\nconversions. Typ only the number corresponding to the conversion.\n\n"; cout << "1. Celsius -> Fahrenheit\n"; cout << "2. Fahrenheit -> Celsius\n"; cout << "3. Kelvin -> Fahrenheit\n"; cout << "4. Fahrenheit -> Kelvin\n"; cout << "5. Celsius -> Kelvin\n"; cout << "6. Kelvin -> Celsius\n\n"; cout << "To exit the program press 'q'\n"; } // Return true if choise is valid. bool Temperature::isvalid(char ch) { if (ch < '1' || ch > '6' && ch != 'q') return false; else return true; } //end of isvalid part // Start main function int main() { char choise; Temperature tmpob; // Create an instance of the Temperature class. // Use the temperature object to display information. for(;;) { do { tmpob.showmenu(); cout << "Type your choise: "; cin >> choise; } while(!tmpob.isvalid(choise)); // While isvalid returns true. if (choise == 'q') break; cout << "\n";
}
tmpob.converter(choise); } return 0;