Dual Purpose Application

Dual purpose Application

Make a program that prompts the user to select what they want to do: calculate or convert units.

Use the calculator code from 1.4.9-1 and the unit conversion code from 1.4.10-1 to create 2 functions.

Once the user makes a selection, call the respective function to execute the desired task.

Hint

Ensure you properly integrate the code from the given tasks into functions before setting up a switch or if statement to execute one of the 2./task.php?id=1_4_10-1/task.php?id=1_4_10-1

#include <iostream>

// These 2 functions need to be created here.
// run_calculator();
// run_unit_converter();

int main() 
{
    std::cout << "Select an option:\n";
    std::cout << "1. Calculator\n";
    std::cout << "2. Unit Converter\n";
    int choice;
    std::cin >> choice;

    switch (choice)
    {
        case 1:
            // Call calculator function
            run_calculator();
            break;
        case 2:
            // Call unit converter function
            run_unit_converter();
            break;
        default:
            std::cout << "Invalid choice.\n";
            break;
    }
}

Answer

#include <iostream>

void run_calculator()
{
    std::cout << "input the operation you want to perform\n1=addition\n2=subtraction\n3=multiplication\n4=division";
    int operation;
    std::cin >> operation;

    std::cout << "input your 2 numbers (seperated by space).\n";
    float input1, input2;
    std::cin >> input1 >> input2;

    if (operation == 1)
    {
        std::cout << input1 << " + " << input2 << " = " << input1 + input2;
    }
    else if (operation == 2)
    {
        std::cout << input1 << " - " << input2 << " = " << input1 - input2;
    }
    else if (operation == 3)
    {
        std::cout << input1 << " * " << input2 << " = " << input1 * input2;
    }
    else if (operation == 4)
    {
        // Make sure that our calculation is safe, and let the user know if it isn't.
        if (input2 != 0)
        {
            std::cout << input1 << " / " << input2 << " = " << input1 / input2;
        }
        else
        {
            std::cout << input1 << " / " << input2 << "Is an unsafe, and invalid calculation.\nYou cannot divide by 0.\n";
        }
    }
    else
    {
        std::cout << operation << " isn't a valid operation. \nPlease stick to the specified avaliable operations.\n";
    }
}

void unit_conversion()
{
     std::cout << "What conversion would you like to make?\n";
    std::cout << "1. km -> (US) miles\n";
    std::cout << "2. (US) miles -> km\n";
    std::cout << "3. meters -> feet\n";
    std::cout << "4. feet -> meters\n";
    std::cout << "5. Celsius -> Fahrenheit\n";
    std::cout << "6. Fahrenheit -> Celsius\n";

    int conversion;
    std::cin >> conversion;

    std::cout << "Enter the value to convert: ";
    float value;
    std::cin >> value;

    switch (conversion)
    {
    case 1: // km to miles
        std::cout << value << " km is " << value * 0.621371 << " miles.\n";
        break;
    case 2: // miles to km
        std::cout << value << " miles is " << value * 1.60934 << " km.\n";
        break;
    case 3: // meters to feet
        std::cout << value << " meters is " << value * 3.28084 << " feet.\n";
        break;
    case 4: // feet to meters
        std::cout << value << " feet is " << value * 0.3048 << " meters.\n";
        break;
    case 5: // Celsius to Fahrenheit
        std::cout << value << " Celsius is " << value * 1.8 + 32 << " Fahrenheit.\n";
        break;
    case 6: // Fahrenheit to Celsius
        std::cout << value << " Fahrenheit is " << (value - 32) / 1.8 << " Celsius.\n";
        break;
    default:
        std::cout << "Error: conversion not recognized.\n";
        break;
    }
}

int main()
{
    std::cout << "Select an option:\n";
    std::cout << "1. Calculator\n";
    std::cout << "2. Unit Converter\n";
    int choice;
    std::cin >> choice;

    switch (choice)
    {
    case 1:
        // Call calculator function
        run_calculator();
        break;
    case 2:
        // Call unit converter function
        run_unit_conversion();
        break;
    default:
        std::cout << "Invalid choice.\n";
        break;
    }
}
Questions