main
1#include <iostream>
2using namespace std;
3
4float c_to_f(float temperature) { return (temperature * (9.0 / 5.0)) + 32.0; }
5
6float f_to_c(float temperature) { return (((temperature - 32.0) * 5.0) / 9.0); }
7
8int main(void) {
9 float temperature;
10 char unit;
11
12 cout << "What is the input temperature? ";
13 cin >> temperature;
14
15 cout << "What are the units of the input temperature (C for Celcius or F for "
16 "Fahrenheit)? ";
17 cin >> unit;
18
19 unit = toupper(unit);
20 switch (unit) {
21 case 'C':
22 cout << "Your input temperature is " << temperature << unit;
23
24 if (temperature < -273) {
25 cout << " which is out of range (less than -273C)." << endl;
26 return 1;
27 }
28
29 cout << " which is " << c_to_f(temperature) << "F" << "." << endl;
30 break;
31
32 case 'F':
33 cout << "Your input temperature is " << temperature << unit;
34
35 if (temperature < -416) {
36 cout << " which is out of range (less than -416F)." << endl;
37 return 2;
38 }
39
40 cout << " which is " << f_to_c(temperature) << "C" << "." << endl;
41 break;
42
43 default:
44 cout << "The units you have specified are not one of C (Celcius) or F "
45 "(Fahrenheit)"
46 << endl;
47 return 3;
48 }
49 return 0;
50}