Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Brett Westbrook
- // COSC 1330 - 003
- // Chapter 9 - Temperature Conversion
- // Converts from Fahrenheit to Celsius and vice versa. (Might implement Kelvin)
- /*
- Formulas
- [°C] = ([°F] - 32) × 5/9 | FAHRENHEIT TO CELSIUS
- [°F] = [°C] × 9/5 + 32 | CELSIUS TO FAHRENHEIT
- */
- #include <iostream>
- #include <iomanip>
- using namespace std;
- char getTempType(char convertSelect);
- double convertToCelsius(double inFahrenheit, double outCelsius);
- double convertToFahrenheit(double inCelsius, double outFahrenheit);
- int main()
- {
- // Variable Declarations
- // Celsius
- double inputCelsius = 0.0;
- double outputCelsius = 0.0;
- // Fahrenheit
- double inputFahrenheit = 0.0;
- double outputFahrenheit = 0.0;
- char selection = ' ';
- selection = getTempType(selection);
- system("PAUSE");
- return 0;
- }
- // Functions
- char getTempType(char convertSelect) // Processes the arithmetic and conversions
- {
- char selection = ' ';
- char loop = ' ';
- // Celsius
- double inputCelsius = 0.0;
- double outputCelsius = 0.0;
- // Fahrenheit
- double inputFahrenheit = 0.0;
- double outputFahrenheit = 0.0;
- do { // begin loop
- cout << "Press 1 to convert from Celsius to Fahrenheit" << endl;
- cout << "Press 2 to convert from Fahrenheit to Celsius" << endl;
- cin >> selection;
- system("CLS");
- // selecting a method of conversion
- switch (selection) {
- case '1':
- outputFahrenheit = convertToFahrenheit(inputCelsius, outputFahrenheit);
- break;
- case '2':
- outputCelsius = convertToCelsius(inputFahrenheit, outputCelsius);
- break;
- default:
- cout << "Invalid input." << endl;
- }
- cout << endl << "Would you like to make another conversion? (Y for yes / N for no)" << endl;
- cin >> loop;
- system("CLS");
- if (loop == 'n' || loop == 'N') {
- cout << "Thanks for using this conversion tool!" << endl;
- }
- } while (loop == 'y' || loop == 'Y'); // end loop
- return selection;
- }
- double convertToFahrenheit(double inCelsius, double outFahrenheit) // CELSIUS TO FAHRENHEIT
- {
- // [°F] = [°C] × 9/5 + 32 | CELSIUS TO FAHRENHEIT
- double inputCelsius = 0.0;
- double outputFahrenheit = 0.0;
- cout << "[Celsius] Enter temperature: ";
- cin >> inputCelsius;
- outputFahrenheit = (((inputCelsius * 9) / 5) + 32);
- cout << "Fahrenheit conversion: " << outputFahrenheit << endl;
- return outputFahrenheit;
- }
- double convertToCelsius(double inFahrenheit, double outCelsius) // FAHRENHEIT TO CELSIUS
- {
- // [°C] = ([°F] - 32) × 5 / 9 | FAHRENHEIT TO CELSIUS
- double inputFahrenheit = 0.0;
- double outputCelsius = 0.0;
- cout << "[Fahrenheit] Enter temperature: ";
- cin >> inputFahrenheit;
- outputCelsius = (((inputFahrenheit - 32) / 9) * 5);
- cout << "Celsius conversion: " << fixed << setprecision(1) << outputCelsius << endl;
- return outputCelsius;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement