Commit 6fcc04e

mo khan <mo@mokhan.ca>
2025-01-10 21:08:06
Use a switch statement
1 parent 78ad43c
projects/1/GoodDocsF2-Darwin.exe
Binary file
projects/1/GoodDocsF2.cpp
@@ -1,72 +1,50 @@
 #include <iostream>
 using namespace std;
 
-/*
-1. F to C
-  > What is the input temperature? 32
-  > What are the units of the input temperature (C for Celcius or F for Fahrenheit)? F
-  > Your input temperature is 32F which is 0C.
+float c_to_f(float temperature) { return (temperature * (9.0 / 5.0)) + 32.0; }
 
-2. C to F
-  > What is the input temperature? 100
-  > What are the units of the input temperature (C for Celcius or F for Fahrenheit)? C
-  > Your input temperature is 100C which is 212F.
-
-3. Out of range C
-  > What is the input temperature? -274
-  > What are the units of the input temperature (C for Celcius or F for Fahrenheit)? C
-  > Your input temperature is -274C which is out of range (less than -273.15C or -416F)
-
-4. Out of range F
-  > What is the input temperature? -417
-  > What are the units of the input temperature (C for Celcius or F for Fahrenheit)? F
-  > Your input temperature is -417 which is out of range (less than -273.15C or -416F)
-
-5. Unknown unit
-  > What is the input temperature? -40
-  > What are the units of the input temperature (C for Celcius or F for Fahrenheit)? Q
-  > The units you have specified are not one of C (Celcius) or F (Fahrenheit).
-*/
+float f_to_c(float temperature) { return (((temperature - 32.0) * 5.0) / 9.0); }
 
 int main(void) {
-  char input_units, output_units;
-  float input_temp, output_temp;
-  int error = 0;
+  float temperature;
+  char unit;
 
   cout << "What is the input temperature? ";
-  cin >> input_temp;
+  cin >> temperature;
 
   cout << "What are the units of the input temperature (C for Celcius or F for "
           "Fahrenheit)? ";
-  cin >> input_units;
-
-  input_units = toupper(input_units);
-  if (input_units == 'C') {
-    cout << "Your input temperature is " << input_temp << input_units;
+  cin >> unit;
 
-    if (input_temp < -273) {
-      cout << " which is out of range (less than -273C or -416F)." << endl;
-    } else {
-      // output_units = 'F';
-      output_temp = (input_temp * (9.0 / 5.0)) + 32.0;
+  unit = toupper(unit);
+  switch (unit) {
+  case 'C':
+    cout << "Your input temperature is " << temperature << unit;
 
-      cout << " which is " << output_temp << "F" << "." << endl;
+    if (temperature < -273) {
+      cout << " which is out of range (less than -273C)." << endl;
+      return 1;
     }
-  } else if (input_units == 'F') {
-    cout << "Your input temperature is " << input_temp << input_units;
 
-    if (input_temp < -416) {
-      cout << " which is out of range (less than -273C or -416F)." << endl;
-    } else {
-      // output_units = 'C';
-      output_temp = (((input_temp - 32.0) * 5.0) / 9.0);
+    cout << " which is " << c_to_f(temperature) << "F" << "." << endl;
+    break;
 
-      cout << " which is " << output_temp << "C" << "." << endl;
+  case 'F':
+    cout << "Your input temperature is " << temperature << unit;
+
+    if (temperature < -416) {
+      cout << " which is out of range (less than -416F)." << endl;
+      return 2;
     }
-  } else {
+
+    cout << " which is " << f_to_c(temperature) << "C" << "." << endl;
+    break;
+
+  default:
     cout << "The units you have specified are not one of C (Celcius) or F "
             "(Fahrenheit)"
          << endl;
+    return 3;
   }
   return 0;
 }
projects/1/GoodDocsF2.exe
Binary file