/* This program converts temperature values from Celsius,Rakine or Fahrenheit into K. It demonstrates the alternative to the Switch/Case construct using If/Else. Copyright C. S. Tritt, Ph.D. Created 10/6/02 Last revised 10/7/02 Version 1.0 */ // Provide ANSI logical operator names. #define and && #define or || #define not ! #include using namespace std; int main() { // Declare variables. double tInput; // Entered temperature in deg. C, R or F. double tOutputK; // Converted temperature in K. char tUnits; // Temperature units (c, C, r, R, f or F). char again; // Repeat indicator. do { // Prompt user and get input. cout << "Enter temperature to convert with units (c, C, r, R, f or F): "; cin >> tInput >> tUnits; // Process input using a switch/case construct. Handle both upper and lower // case letters. if (tUnits == 'c' or tUnits == 'C') tOutputK = tInput + 273.15; // Convert C to K. else if (tUnits == 'r' or tUnits == 'R') tOutputK = tInput * 5. / 9.; // Convert R to K. else if (tUnits == 'f' or tUnits == 'F') tOutputK = (tInput + 459.67) * 5. / 9.; // Convert F to K. else { cout << "Temperature units must be c, C, r, R, f or F. Aborting.\n"; return 1; } // Display results. if (tOutputK < 0) // Valid input produces non-negative output. { cout << "Invalid input. Aborting.\n"; return 2; } cout << "is " << tOutputK << " K.\n"; // Check if user wants to go again. cout << "Go again (\"y\" or \"n\")? "; // cin >> again; again = 'y'; } while (again == 'y' or again == 'Y'); return 0; }