Monday, December 4, 2017

Programming Challenge 14.1 - Numbers Class

Example Files: NumbersClass.7z

Numbers.h


#ifndef NUMBERS_H_
#define NUMBERS_H_

#include <string>
using std::string;

class Numbers
{
    private:
        int number;                                    // To hold a number in the range 0 through 9999

        static string zeroToTwenty[20];       // To hold number words in the range 0 through 20
        static string tenMult[10];                // To hold number words for multiples of 10
        static string hundred;
        static string thousand;

    public:
        Numbers(int);                           

        string describeTens(int);
        void print();

        int getNumber() const
        { return number; }
};

#endif

Numbers.cpp


#include "Numbers.h"

#include <iostream>
using std::cout;

// Definition of static string objects
string Numbers::zeroToTwenty[] = { "zero", "one", "two", "three", "four", "five", "six", "seven",
                                                 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
                                                 "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

string Numbers::tenMult[] = { "", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
                                        "eighty", "ninety" };

string Numbers::hundred = "hundred";
string Numbers::thousand = "thousand";

/* **********************************************************
            Numbers::Numbers() : int - Constructor
    The constructor accepts an integer as argument, which is
    used to initialize the
   ********************************************************** */

Numbers::Numbers(int n)
{
    number = n;
}

/* **********************************************************
            Numbers::describeTens() : int
    The integer passed to this function is used as an array
    index, allowing the retrieval and assignment of a number
    word to a temporary string object. This object's contents
    is returned from the function.
   ********************************************************** */

string Numbers::describeTens(int n)
{
    string tmpTens = "";

    if (n < 20)
    {
        return tmpTens = zeroToTwenty[n];
    }
    else if (n >= 20 && n % 10 == 0)
    {
        return tmpTens = tenMult[n / 10];
    }
    else
    {
        return tmpTens = tenMult[n / 10] + " " + zeroToTwenty[n % 10];
    }
}

/* **********************************************************
            Numbers::print()
    This function translates a dollar amount in the range of 0
    through 9999 into an English description, and outputs it
    to screen.
   ********************************************************** */

void Numbers::print()
{
    string description = "";

    if (getNumber() < 100)
    {
        description.append(describeTens(getNumber()));
    }   
    else if (getNumber() < 1000)
    {
        description.append(zeroToTwenty[(getNumber() / 100)] + " " + hundred + " " +
                                 describeTens(getNumber() % 100));
    }
    if (getNumber() > 999)
    {
        description.append(zeroToTwenty[getNumber() / 1000] + " " + thousand + ", ");

        if (getNumber() % 1000 < 100)
        {
            description.append(describeTens(getNumber() % 1000));
        }
        else
        {
            description.append(zeroToTwenty[(getNumber() % 1000) / 100] + " " + hundred + " and " +
                                     describeTens(getNumber() % 100));
        }
    }
   
    cout << "\nThis is your dollar amount converted to English words:\n";
    cout << "----------------------------------------------------\n";
    cout << description << " dollar(s)\n";
}

NumbersClassDm.cpp


#include "Numbers.h"

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

int main()
{
    char again = ' ';            // Temporary variable holding a user's choice
    int tempNum = 0;            // Temporary variable to hold a number in an approriate range

    cout << "\tDOLLAR AMOUNT TO ENGLISH WORD CONVERTER\n";

    do
    {
        cout << "\nPlease enter a dollar amount in the range of 0 through 9999: ";
        cin >> tempNum;

        while (tempNum < 0 || tempNum > 9999)
        {
            cout << "\nInput Error!\n";
            cout << "Please enter a dollar amount in the range of 0 through 9999: ";
            cin >> tempNum;
        }

        // Create a Numbers class object
        Numbers number(tempNum);
        number.print();

        cout << "\nDo you wish to convert another dollar amount (y/N)? ";
        cin >> again;

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            cout << "\nDo you wish to convert another dollar amount (y/N)? ";
            cin >> again;
        }

        if (toupper(again) == 'N')
        {
            cout << "\nThank you for trying this demo program. Have a nice day!\n";
        }

    } while (toupper(again) == 'Y');


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

Example Output:






1 comment: