4.11: C++ Examples
- Page ID
- 10647
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
Temperature
The temperature program main function is listed twice, once using if-then-else and once using switch, followed by the supporting functions.
If Then Else
// This program asks the user to select Fahrenheit or Celsius conversion // and input a given temperature. Then the program converts the given // temperature and displays the result. // // References: // https://www.mathsisfun.com/temperature-conversion.html // https://en.wikibooks.org/wiki/C%2B%2B_Programming #include using namespace std; char getChoice(); void processCelsius(); void processFahrenheit(); double getTemperature(string scale); double calculateCelsius(double fahrenheit); double calculateFahrenheit(double celsius); void displayResult(double temperature, string fromScale, double result, string toScale); int main() { char choice; choice = getChoice(); if (choice == 'C' || choice == 'c') { processCelsius(); } else if (choice == 'F' || choice == 'f') { processFahrenheit(); } else { cout << "You must enter C to convert to Celsius or F to convert to Fahrenheit." << endl; } return 0; }
Switch
int main() { char choice; choice = getChoice(); switch(choice) { case 'C': case 'c': processCelsius(); break; case 'F': case 'f': processFahrenheit(); break; default: cout << "You must enter C to convert to Celsius or F to convert to Fahrenheit." << endl; } return 0; }
Supporting Functions
char getChoice() { char choice; cout << "Enter C to convert to Celsius or F to convert to Fahrenheit:" << endl; cin >> choice; return choice; } void processCelsius() { double temperature; double result; temperature = getTemperature("Fahrenheit"); result = calculateCelsius(temperature); displayResult(temperature, "Fahrenheit", result, "Celsius"); } void processFahrenheit() { double temperature; double result; temperature = getTemperature("Celsius"); result = calculateFahrenheit(temperature); displayResult(temperature, "Celsius", result, "Fahrenheit"); } double getTemperature(string scale) { double temperature; cout << "Enter " << scale << " temperature:" << endl; cin >> temperature; return temperature; } double calculateCelsius(double fahrenheit) { double celsius; celsius = (fahrenheit - 32) * 5 / 9; return celsius; } double calculateFahrenheit(double celsius) { double fahrenheit; fahrenheit = celsius * 9 / 5 + 32; return fahrenheit; } void displayResult(double temperature, string fromScale, double result, string toScale) { cout << temperature << "° " << fromScale << " is " << result << "° " << toScale << endl; }
Output
Enter C to convert to Celsius or F to convert to Fahrenheit: c Enter Fahrenheit temperature: 100 100° Fahrenheit is 37.7778° Celsius Enter C to convert to Celsius or F to convert to Fahrenheit: f Enter Celsius temperature: 100 100° Celsius is 212° Fahrenheit Enter C to convert to Celsius or F to convert to Fahrenheit: x You must enter C to convert to Celsius or F to convert to Fahrenheit.
References
- Wikiversity: Computer Programming