# Applied Programming/Loops/C++

## loops.cpp

```/*
This program converts a Fahrenheit temperature to Celsius.

Input:
Fahrenheit temperature

Output :
Fahrenheit temperature
Celsius temperature

Example :
Enter Fahrenheit temperature or press <Enter> to quit : 100

100.0 Fahrenheit is 37.8 Celsius!

F   C
100.0   37.8
101.0   38.3
102.0   38.9
103.0   39.4
104.0   40.0
105.0   40.6
106.0   41.1
107.0   41.7
108.0   42.2
109.0   42.8
110.0   43.3

Enter Fahrenheit temperature or press <Enter> to quit :

References :
https://stackoverflow.com/questions/7663709/how-can-i-convert-a-stdstring-to-int
https://en.cppreference.com/w/cpp/language/try_catch
https://www.geeksforgeeks.org/range-based-loop-c/

*/

#include <string>
#include <iostream>
using namespace std;

int ABSOLUTE_ZERO = -459.67;

double get_fahrenheit()
/*Gets Fahrenheit temperature.

Args:
None

Returns:
double: Fahrenheit temperature or "" if no temperature entered.

*/
{
while (true){
string fahrenheit;
printf("\n\nEnter Fahrenheit temperature or press <Enter> to quit: ");
getline(cin, fahrenheit);
try {
if (fahrenheit == "") {
return 0;
} else if (std::stod(fahrenheit) <= ABSOLUTE_ZERO){
printf("\nError: Fahrenheit temperature cannot be below absolute zero! (-459.67)");
} else {
return std::stod(fahrenheit);
}
} catch(...) {
cout << "\n" << "Error: |" << fahrenheit << "| is not a valid input!";
}
}
}

float fahrenheit_to_celsius(double fahrenheit)
/*Converts Fahrenheit temperature to Celsius.

Args:
fahrenheit (double): Fahrenheit temperature to be converted

Returns:
double: Celsius temperature

Raises:
Error: A catastrophic error has occored!

*/
{
int TEMPERATURE_DIFFERENCE = 32;
double RATIO_ONE = 5;
double RATIO_TWO = 9;
double TEMPEATURE_RATIO = RATIO_ONE / RATIO_TWO;

try {
double celsius = (fahrenheit - TEMPERATURE_DIFFERENCE) * TEMPEATURE_RATIO;
return celsius;
} catch(...) {
printf("Error: A catastrophic error has occored!");
}
}

void display_results(double fahrenheit, double celsius)
/*Displays Fahrenheit and Celsius temperatures.

Args:
fahrenheit (double): Fahrenheit temperature
celsuis (double): Celsius temperature

Returns:
None

*/
{
printf("\n%.1f Fahrenheit is %.1f Celsius!\n", fahrenheit, celsius);
}

void display_table(double fahrenheit)
/*Displays nearest multiples of 10 Fahrenheit and Celsius temperatures.

Args:
fahrenheit (float): Fahrenheit temperature

Returns:
None

*/
{
printf("\nF\tC");
int start = int(fahrenheit / 10) * 10;
int end = int(fahrenheit + 11);
for (start; fahrenheit < end; fahrenheit += 1){
double celsius = fahrenheit_to_celsius(fahrenheit);
printf("\n%.1f\t", fahrenheit);
printf("%.1f", celsius);
}
}

int main()
// Runs the main program logic. //
{
while (true){
try {
double fahrenheit = get_fahrenheit();
if (fahrenheit == 0) {
printf("\nExiting...");
break;
}
float celsius = fahrenheit_to_celsius(fahrenheit);
display_results(fahrenheit, celsius);
display_table(fahrenheit);
} catch(...) {
printf("An unexpected error has occurred!");
printf("\nExiting...");
break;
}
}
}
```

## Try It

Copy and paste the code above into one of the following free online development environments or use your own C++ compiler / interpreter / IDE. Applied Programming/IDE/C++/Online/Free