/* This program converts temperature values from Celsius,Rakine or Fahrenheit into K. It demonstrates the use of the Switch/Case construct. Copyright C. S. Tritt, Ph.D. Created 10/6/02 Last revised 10/6/02 Version 1.1 */ // 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. switch (tUnits) { case 'c': case 'C': tOutputK = tInput + 273.15; // Convert deg. C to K. break; case 'r': case 'R': tOutputK = tInput * 5. / 9.; // Convert deg. R to K. break; case 'F': case 'f': tOutputK = (tInput + 459.67) * 5. / 9.; // Convert deg. F to K. break; default: cout << "Temperature units must be c, C, r, R, f or F. Aborting.\n"; return 1; // No break required here because of unconditional return. } // End of switch/case. // Display results. if (tOutputK < 0) // Valid input produces non-negative output. { cout << "Invalid input. Aborting.\n"; return 2; } cout << "is " << tOutputK << " K.\n"; cout << "Go again (\"y\" or \"n\")? "; cin >> again; } while (again == 'y' or again == 'Y'); return 0; }