The switch Statement Selection Revisited
Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion (Celsius-to-Kelvin, Kelvin-to-Celsius, Fahrenheit-to-Celsius, Celsius-to-Fahrenheit, Fahrenheit-to-Kelvin, or Kelvin-to-Fahrenheit).
Preliminary Analysis One way to solve the problem is to provide a menu of choices, from which the user selects the operation they want to perform: Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit -->
Behavior: main() Our program should display its purpose and a menu of choices. It should then read the user’s choice. It should then prompt for and read the temperature to be converted. It should then compute the converted temperature by applying to the input temperature the particular conversion specified by the user’s choice. It should then display the converted temperature.
Objects: main() Description Type Kind Name menu string constant MENU choice char variable choice input temp. double variable tempIn output temp. double variable tempOut purpose string constant --
Operations: main() Description Predefined? Library? Name display strings yes iostream << read a double yes iostream >> convert a temp. no display double yes iostream <<
Algorithm: main() 0. Display via cout the purpose of the program. 1. Loop a. Display MENU via cout. b. Read choice from cin. c. If choice is ‘q’, terminate repetition. d. Prompt for and read tempIn from cin. e. Compute tempOut by converting tempIn using choice. f. Display tempOut. End loop. End loop.
Organization We’ll need: –a main function –six conversion functions –a function to apply the right conversion The conversion functions seem likely to be reuseable someday, so we’ll create a library named Heat in which to store them.
Behavior: FahrenheitToCelsius() Our function should receive from its caller the temperature to be converted. It should return the result of subtracting 32.0 from that temperature, and then dividing the difference by 1.8.
Objects: FahrenheitToCelsius() Description Type Kind Name the converted double variable -- temperature the original double variable originalTemp temperature We can use this as a pattern for each of the temperature conversion functions -- they differ only in the particular formula they use.
Coding: Conversion Prototypes /* Heat.h *... */ double FahrenheitToCelsius(double originalTemp); double CelsiusToFahrenheit(double originalTemp); double FahrenheitToKelvin(double originalTemp); double KelvinToFahrenheit(double originalTemp); double CelsiusToKelvin(double originalTemp); double KelvinToCelsius(double originalTemp);
Operations: FahrenheitToCelsius() Description Predefined? Library? Name receive a double yes built-in subtract doubles yes built-in - divide doubles yes built-in / return a double yes built-in return
Algorithm: FahrenheitToCelsius() 0. Receive originalTemp. 1. Return (originalTemp ) / 1.8. We can use this algorithm as a pattern for each of the temperature conversion functions -- each differs only in the particular formula used.
Coding: Conversion Definitions /* Heat.cpp *... */ double FahrenheitToCelsius(double originalTemp) { return (originalTemp ) / 1.8; } double CelsiusToFahrenheit(double originalTemp) { return originalTemp * ; } double FahrenheitToKelvin(double originalTemp) { return (originalTemp ) / ; } //... other conversion function definitions
Organization (ii) Once our library is completed, we can design and implement a function to perform the particular conversion specified by the user’s menu choice. We will call this function Convert(). Since it is less likely to be reuseable than our conversion functions, we will store its prototype and definition in the same file as our main function.
Behavior: Convert() Our function should receive from its caller the temperature to be converted and the user’s menu choice. Based on the menu choice, our function should return the result of applying the appropriate temperature conversion function to the received temperature.
Objects: Convert() Description Type Kind Name the converted double variable -- temperature the temp. double variable origTemp being converted Our prototype is thus: double Convert(double origTemp, char choice); a menu choice char variable choice
Operations: Convert() Description Defined? Library? Name return a double yes built-in return convert F-to-C yes Heat... convert C-to-F yes Heat... convert F-to-K yes Heat... convert K-to-F yes Heat... convert C-to-K yes Heat... convert K-to-C yes Heat... perform selection yes built-in switch based on a char
Algorithm: Convert() 0. Receive origTemp, choice. 1. If choice is ‘a’: return FahrenheitToCelsius(origTemp); Else if choice is ‘b’: return CelsiusToFahrenheit(origTemp); Else if choice is ‘b’: return CelsiusToFahrenheit(origTemp); Else if choice is ‘c’: return FahrenheitToKelvin(origTemp); Else if choice is ‘c’: return FahrenheitToKelvin(origTemp); Else if choice is ‘d’: return KelvinToFahrenheit(origTemp); Else if choice is ‘d’: return KelvinToFahrenheit(origTemp); Else if choice is ‘e’: return CelsiusToKelvin(origTemp); Else if choice is ‘e’: return CelsiusToKelvin(origTemp); Else if choice is ‘f’: return KelvinToCelsius(origTemp); Else if choice is ‘f’: return KelvinToCelsius(origTemp); Else display error message and return 0.0. Else display error message and return 0.0. We could implement this algorithm with a multi-branch if, but let’s learn something new.
Coding: Convert() /* main.cpp *... */ // rest of main.cpp double Convert(double origTemp, char choice) { switch (choice) { case ‘a’: return FahrenheitToCelsius(origTemp); case ‘b’: return CelsiusToFahrenheit(origTemp); case ‘c’: return FahrenheitToKelvin(origTemp); case ‘d’: return KelvinToFahrenheit(origTemp); case ‘e’: return CelsiusToKelvin(origTemp); case ‘f’: return KelvinToCelsius(origTemp); default : cerr << “\nUnsupported menu choice: “ << choice << “ in Convert()” << endl; return 0.0; }
Discussion double Convert(double origTemp, char choice) { switch (choice) { case ‘a’: return FahrenheitToCelsius(origTemp); case ‘b’: return CelsiusToFahrenheit(origTemp); case ‘c’: return FahrenheitToKelvin(origTemp); case ‘d’: return KelvinToFahrenheit(origTemp); case ‘e’: return CelsiusToKelvin(origTemp); case ‘f’: return KelvinToCelsius(origTemp); default : cerr << “\nUnsupported menu choice: “ << choice << “ in Convert()” << endl; return 0.0; } The switch statement provides an alternative means of doing multi-branch selection.
Organization (iii) Once our library and conversion function have been designed and completed, we can proceed to implement the algorithm for our main function. The primary control structure is a forever loop that continues repetition until the user enters menu choice ‘q’...
Coding: main() #include // cin, cout, >,... using namespace std; #include “Heat.h” // FahrenheitToCelsius(),... double Convert(double originalTemp, char menuChoice); int main() { const MENU = “\nPlease enter:\n“ “ a - to convert Fahrenheit to Celsius\n” “ b - to convert Celsius to Fahrenheit\n” “ c - to convert Fahrenheit to Kelvin\n” “ d - to convert Kelvin to Fahrenheit\n” “ e - to convert Celsius to Kelvin\n” “ f - to convert Kelvin to Celsius\n” “ q - to quit\n” “--> “; cout << “\nThis program converts temperatures.\n”;
Coding: main() char choice; double tempIn, tempOut; for (;;) { cout << MENU; cin >> choice; if (choice == ‘q’ || choice == ‘Q’) break; cout << “\nEnter the temperature to be converted: “; cin >> tempIn; tempOut = Convert(tempIn, choice); cout << “\nThe converted temperature is “ << tempOut << endl; }
Testing This program converts temperatures. Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> a Enter the temperature to be converted: 212 The converted temperature is 100
Testing Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> b Enter the temperature to be converted: 0 The converted temperature is 32
Testing Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> c Enter the temperature to be converted: 32 The converted temperature is
Summary To perform multibranch selection, C++ provides an alternative statement called the switch statement. We’ll learn more of the details of the if and switch statements next time, including when you should use which statement.