Programming Fundamentals/Conditions/C++

From Wikiversity
Jump to navigation Jump to search

conditions.cpp[edit]

// This program asks the user to select Fahrenheit or Celsius conversion
// and input a given temperature. Then the program converts the given 
// temperature and displays the result.
//
// References:
//     https://www.mathsisfun.com/temperature-conversion.html
//     https://en.wikibooks.org/wiki/C%2B%2B_Programming

#include <iostream>

using namespace std;

void ifElse();
void switchCase();
void processCelsius();
void processFahrenheit();
double getTemperature(string scale);
double calculateCelsius(double fahrenheit);
double calculateFahrenheit(double celsius);
void displayResult(double temperature, string from, double result, string to);

int main()
{
    // main could either be an if-else structure or a switch-case structure
    ifElse();
    switchCase();
}

void ifElse()
{
    char choice;
    
    cout << "Enter F to convert to Fahrenheit or C to convert to Celsius:" << endl;
    cin >> choice;
    if (choice == 'C' || choice == 'c')
    {
        processCelsius();
    }
    else if (choice == 'F' || choice == 'f')
    {
        processFahrenheit();
    }
    else
    {
        cout << "You must enter C to convert to Celsius or F to convert to Fahrenheit!" << endl;
    }
}

void switchCase()
{
    char choice;
    
    cout << "Enter F to convert to Fahrenheit or C to convert to Celsius:" << endl;
    cin >> choice;
    switch(choice)
    {
        case 'C':
        case 'c':
            processCelsius();
            break;
        case 'F':
        case 'f':
            processFahrenheit();
            break;
        default:
            cout << "You must enter C to convert to Celsius or F to convert to Fahrenheit!" << endl;
    }
}

void processCelsius()
{
    double temperature;
    double result;

    temperature = getTemperature("Fahrenheit");
    result = calculateCelsius(temperature);
    displayResult(temperature, "Fahrenheit", result, "Celsius");
}

void processFahrenheit()
{
    double temperature;
    double result;
    
    temperature = getTemperature("Celsius");
    result = calculateFahrenheit(temperature);
    displayResult(temperature, "Celsius", result, "Fahrenheit");
}

double getTemperature(string scale) {
    double temperature;
    
    cout << "Enter " << scale << " temperature:" << endl;
    cin >> temperature;
    
    return temperature;
}

double calculateCelsius(double fahrenheit) {
    double celsius;
    
    celsius = (fahrenheit - 32) * 5 / 9;
    
    return celsius;
}

double calculateFahrenheit(double celsius) {
    double fahrenheit;
    
    fahrenheit = celsius * 9 / 5 + 32;
    
    return fahrenheit;
}

void displayResult(double temperature, string from, double result, string to) {
    cout << temperature << "° " << from << " is " << result << "° " << to << endl;
}

Try It[edit]

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

See Also[edit]