Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- //Fahrenheit-Celcius
- //Convert Fahrenheit to Celsius and vice-versa
- //Simple menu to choose conversion and stop or run again
- void runMenu();
- char userInteraction();
- void displayResults(char menuChoice);
- double calcFahrtoCel(double fahrValue);
- double calcCeltoFahr(double celValue);
- int main()
- {
- runMenu();
- }
- // Run the program, then ask to run it again
- void runMenu()
- {
- displayResults(userInteraction());
- char runAgain = 'n';
- cout << "Would you like to do another conversion? (y or n)" << endl;
- cin >> runAgain;
- switch(runAgain)
- {
- case 'y':
- case 'Y':
- runMenu();
- break;
- default:
- break;
- }
- }
- // Ask which conversion: c-f or f-c. Return char f or c
- char userInteraction()
- {
- char menuChoice = 'x';
- cout << "Convert Fahrenheit to Celsius or Celsius to Fahrenheit" << endl;
- cout << "Press f for Fahrenheit to Celsius" << endl;
- cout << "Press c for Celsius to Fahrenheit" << endl;
- cin >> menuChoice;
- return menuChoice;
- }
- // Take input for conversion, then run calculation
- void displayResults(char menuChoice)
- {
- double fahrInput = 0.0;
- double celInput = 0.0;
- switch(menuChoice)
- {
- case 'f':
- case 'F':
- cout << "Enter Fahrenheit value: " << endl;
- cin >> fahrInput;
- cout << fahrInput << " Fahrenheit = "
- << calcFahrtoCel(fahrInput) << " Celsius" << endl;
- break;
- case 'c':
- case 'C':
- cout << "Enter Celsius value: " << endl;
- cin >> celInput;
- cout << celInput << " Celsius = "
- << calcCeltoFahr(celInput) << " Fahrenheit" << endl;
- break;
- default:
- cout << "Illegal option. Please press only f or c" << endl;
- }
- }
- // Calculate Fahrenheit to Celsius
- double calcFahrtoCel(double fahrValue)
- {
- double celValue = (fahrValue-32.0)*(5.0/9.0);
- return celValue;
- }
- // Calculate Celsius to Fahrenheit
- double calcCeltoFahr(double celValue)
- {
- double fahrValue = celValue*(9.0/5.0)+32.0;
- return fahrValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement