diff options
| author | mo khan <mo@mokhan.ca> | 2025-01-10 14:08:06 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-01-10 14:08:06 -0700 |
| commit | 6fcc04e64e4c83d01a72161f762a676bb840f343 (patch) | |
| tree | f9eee2f4eedfc3df4d299fbd85b7059a41d7cc60 /projects/1 | |
| parent | 78ad43c28696d68a19effc2ebcdd6d816689e7bf (diff) | |
Use a switch statement
Diffstat (limited to 'projects/1')
| -rwxr-xr-x | projects/1/GoodDocsF2-Darwin.exe | bin | 9904 -> 10000 bytes | |||
| -rw-r--r-- | projects/1/GoodDocsF2.cpp | 76 | ||||
| -rwxr-xr-x | projects/1/GoodDocsF2.exe | bin | 145461 -> 145519 bytes |
3 files changed, 27 insertions, 49 deletions
diff --git a/projects/1/GoodDocsF2-Darwin.exe b/projects/1/GoodDocsF2-Darwin.exe Binary files differindex 60b6389..e4b3ea4 100755 --- a/projects/1/GoodDocsF2-Darwin.exe +++ b/projects/1/GoodDocsF2-Darwin.exe diff --git a/projects/1/GoodDocsF2.cpp b/projects/1/GoodDocsF2.cpp index cde052b..37adfb5 100644 --- a/projects/1/GoodDocsF2.cpp +++ b/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; } diff --git a/projects/1/GoodDocsF2.exe b/projects/1/GoodDocsF2.exe Binary files differindex 2a81192..e569895 100755 --- a/projects/1/GoodDocsF2.exe +++ b/projects/1/GoodDocsF2.exe |
