summaryrefslogtreecommitdiff
path: root/projects/1/GoodDocsF2.cpp
blob: 37adfb5fb00b06541c1001660f50388fe58c93c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

float c_to_f(float temperature) { return (temperature * (9.0 / 5.0)) + 32.0; }

float f_to_c(float temperature) { return (((temperature - 32.0) * 5.0) / 9.0); }

int main(void) {
  float temperature;
  char unit;

  cout << "What is the input temperature? ";
  cin >> temperature;

  cout << "What are the units of the input temperature (C for Celcius or F for "
          "Fahrenheit)? ";
  cin >> unit;

  unit = toupper(unit);
  switch (unit) {
  case 'C':
    cout << "Your input temperature is " << temperature << unit;

    if (temperature < -273) {
      cout << " which is out of range (less than -273C)." << endl;
      return 1;
    }

    cout << " which is " << c_to_f(temperature) << "F" << "." << endl;
    break;

  case 'F':
    cout << "Your input temperature is " << temperature << unit;

    if (temperature < -416) {
      cout << " which is out of range (less than -416F)." << endl;
      return 2;
    }

    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;
}