Tuesday, February 20, 2018

Programming Challenge 16.4 - Absolute Value Template

Example File: AbsoluteValue.cpp

AbsoluteValue.cpp


#include <cmath>
using std::abs;

#include <iostream>
using std::cin;
using std::cout;

/* **********************************************************
            template <class T>
    This function returns the absolute value of the parameter
    passed to it.
    ********************************************************** */

template <class T>
T absoluteVal(const T &value)
{
    return abs(value);
}

enum MenuOptions { INT = 'I', DOUBLE = 'D', QUIT = 'Q' };

/* **********************************************************
            Function Definitions  
   ********************************************************** */

void outputOptions();
bool isValidInput(char);
void absoluteDouble();
void absoluteInteger();

int main()
{
    char choice = ' ';

    cout << "ABSOLUTE VALUES\n\n";
    cout << "This program finds the absolute value of a number.\n";
    cout << "Example: The absolute value of |-5.7| is 5.7\n\n";

    do
    {
        outputOptions();
        cin >> choice;
        choice = toupper(choice);
       
        while (isValidInput(choice) == false)
        {
            cout << "Choice: ";
            cin >> choice;
            choice = toupper(choice);
        }           

        switch (choice)
        {       
            case DOUBLE:
            {
                absoluteDouble();
            } break;
           
            case INT:
            {
                absoluteInteger();
            } break;       

            case QUIT:
            {
                cout << "\nThank you for trying this program. Have a nice day!";
            } break;
        }
    } while (choice != QUIT);

    cin.ignore();
    cin.get();
    return 0;
}

/* **********************************************************
            outputOptions()
    Outputs the menu options to screen.
   ********************************************************** */

void outputOptions()
{
    cout << "Do you wish to enter a floating-point or integer value?\n\n"
          << "'D' - Floating-Point Value\n"
          << "'I' - Integer Value\n"
          << "'Q' - Quit Program\n\n";
    cout << "Choice: ";
}

/* **********************************************************
            isValidInput() : char
    Validates the input and returns true if it is, otherwise
    false is returned.
   ********************************************************** */

bool isValidInput(char choice)
{
    if (choice == INT || choice == DOUBLE || choice == QUIT)
    {
        return true;
    }

    return false;
}

/* **********************************************************
            absoluteDouble()
    The user is asked to enter a floating-point value. The
    value is passed to a template function which returns the
    absolute value. The absolute value is output to screen.
   ********************************************************** */

void absoluteDouble()
{
    double floatingPtVal = 0.0;

    cout << "\nEnter a floating-point value: ";
    cin >> floatingPtVal;

    cout << "The absolute value of |" << floatingPtVal << "|"
          << " is: " << absoluteVal(floatingPtVal) << "\n\n";
}

/* **********************************************************
            absoluteInteger()
    The user is asked to enter an integer value. The value is
    passed to a template function which returns the absolute
    value. The absolute value is output to screen.
   ********************************************************** */

void absoluteInteger()
{
    int integerVal = 0;
    cout << "\nEnter an integer value: ";
    cin >> integerVal;

    cout << "The absolute value of |" << integerVal << "|"
          << " is " << absoluteVal(integerVal) << "\n\n";
}

Example Output:



No comments:

Post a Comment