Wednesday, February 21, 2018

Programming Challenge 16.6 - IntArray Class Exception

Example Files: IntArrayClassException.7z

Notice: Most of the code is taken from chapter 14, pp. 858 - 863 in the 9th Edition, or pp. 853 through 857 in the 8th Edition of the book. Notice also that there are two versions of the driver program for this programming challenge. One is listed in this solution, the other is contained in the Example Files archive and is called called "IntArrayExceptionSimple.cpp." The last screenshot demonstrates the output of this second driver program.

IntArray.h


#ifndef INT_ARRAY_H_
#define INT_ARRAY_H_

class IntArray
{
    private:
        int *aptr;
        int arraySize;

        void IntArray::subscriptError(const int &);

    public:
        // Exception class - Handles invalid subscripts
        class InvalidSubscript
        {
            private:
                int errSub;

            public:
                InvalidSubscript(const int &errorVal)
                { errSub = errorVal; }

                int getErrSubscript() const
                { return errSub; }
        };

        IntArray(int);
        IntArray(const IntArray &);
        ~IntArray();

        // Accessor function
        int size() const
        { return arraySize; }

        int &operator [](const int &sub);
};

#endif

IntArray.cpp


#include "IntArray.h"

#include <iostream>
using std::cout;

/* **********************************************************
            IntArray::IntArray() - Constructor
    Sets the size of the array and allocates memory for it.
   ********************************************************** */

IntArray::IntArray(int arrSize)
{
    arraySize = arrSize;
    aptr = new int[arrSize];

    for (int count = 0; count < arraySize; count++)
    {
        *(aptr + count) = 0;
    }
}

/* **********************************************************
            IntArray::IntArray() - Copy Constructor
   ********************************************************** */

IntArray::IntArray(const IntArray &obj)
{
    arraySize = obj.arraySize;
    aptr = new int[arraySize];

    for (int count = 0; count < arraySize; count++)
    {
        *(aptr + count) = *(obj.aptr + count);
    }
}

/* **********************************************************
            IntArray::~IntArray() - Destructor
   ********************************************************** */

IntArray::~IntArray()
{
    if (arraySize > 0)
    {
        delete[] aptr;
    }
}

/* **********************************************************
            IntArray::subscriptError() : const int &
    InvalidSubscript is thrown if the value passed to this
    function is invalid.
   ********************************************************** */

void IntArray::subscriptError(const int &sub)
{
    if (sub < 0 || sub >= arraySize)
    {
        throw InvalidSubscript(sub);
    }
}

/* **********************************************************
            IntArray::operator []() : const int &
    The function makes a call to the subscriptError(). If no
    error is thrown, a reference to the element in the array
    indexed by the subscript is returned.
   ********************************************************** */

int &IntArray::operator [](const int &sub)
{
    subscriptError(sub);

    return aptr[sub];
}

IntArrayException.cpp


#include "IntArray.h"

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

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

void outputIntro();
void playGame(IntArray &, const int &, const int);
void retrieveValue(IntArray &, const int &, const int);
bool isCorrect(const int &, const int &, const int);

int main()
{
    const int SIZE = 10;
    int         multBy = 0;

    IntArray table(SIZE);

    cout << "MULTIPLICATION GAME\n\n";
    outputIntro();

    cout << "Select a difficulty 1 through " << SIZE << ": ";
    cin >> multBy;

    playGame(table, multBy, SIZE);
    retrieveValue(table, multBy, SIZE);

    cout << "This is the end of the game little Math-Warrior!\n"
          << "You did great! Come and play again!";

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

/* **********************************************************
            outputIntro()
    Outputs an introduction to the game.
   ********************************************************** */

void outputIntro()
{
    cout << "Welcome, little Math-Warrior! In this game you are about to exercise\n"
          << "your multiplication skills. Enter the multiplication number to hone your\n"
          << "superior skills on. If you enter 2, you have to multiply this number by\n"
          << "1 through 10.\n\n";
    cout << "Example: How much is 2 x 5:\n\n";
    cout << "When you are asked, type in the answer you think is correct. If you make\n"
          << "a mistake, you can retry until the answer is correct. You can do it!\n";
    cout << "Have fun, little Math-Warrior!\n\n";
}

/* **********************************************************
            playGame() : obj &, const int &, const int
    The little Math-Warrior is asked to input the result of a
    multiplication. If the answer is correct, the value is
    copied to the array. Otherwise, he or she is asked to
    enter a valid answer. Once all values are entered, a
    congratulatory message is output and the function exits.
   ********************************************************** */

void playGame(IntArray &table, const int &multBy, const int SIZE)
{
    int result = 0;

    cout << "\nMultiplication-Table: " << multBy << "'s\n\n";
    for (int x = 0; x < SIZE; x++)
    {
        cout << "How much is " << (x + 1) << " x " << multBy << ": ";
        cin >> result;

        while (isCorrect(multBy, result, x + 1) == false)
        {
            cout << "\nTry again, little Math-Warrior!\n";
            cout << (x + 1) << " x " << multBy << " is: ";
            cin >> result;
        }

        if (x == SIZE - 1)
        {
            cout << "\nWELL DONE LITTLE MATH WARRIOR YOU BEAT THIS LEVEL!\n";
        }

        table[x] = result;
    }
}

/* **********************************************************
            isCorrect() : const int &, const int &, const int
    This function validates the correctness of the calculation
    result. If it is, true is returned, otherwise false is
    returned.
   ********************************************************** */

bool isCorrect(const int &multBy, const int &result, const int idx)
{
    bool status = false;

    int calc = multBy * idx;

    if (result == calc)
    {
        cout << "WELL DONE!\n\n";
        status = true;
    }

    return status;
}

/* **********************************************************
            retrieveValue() : obj &, const int, const int &
    This function lets our little Math-Warriors do a review.
    He or she is asked to enter the subscript position of the
    array holding the results. If it is correct, the function
    carries out the calculation and outputs the result. If it
    isn't the try/catch block is executed, until a valid
    subscript position has been provided.
   ********************************************************** */

void retrieveValue(IntArray &table, const int &multBy, const int arrSize)
{
    bool tryAgain = true;
    int  subPosition = 0;

    cout << "TIME FOR A REVIEW!\n\n";
    cout << "Enter a number 1 through 10: ";
    cin >> subPosition;

    while (tryAgain)
    {
        try
        {
            cout << "\nThe result of multiplying "
                  << multBy << " x " << subPosition
                  << " is: " << table[subPosition - 1] << "\n\n";
            tryAgain = false;
        }
        catch (IntArray::InvalidSubscript sub)
        {
            cout << "\nThere was an error, little Math-Warrior!\n"
                  << multBy << " x " << sub.getErrSubscript()+1 << " is invalid.\n";
            cout << "This multiplication table goes from 1 through " << arrSize << "\n";
            cout << "Enter a valid number [1 through " << (arrSize) << "]: ";
            cin >> subPosition;
        }
    }
}

Example Output: 







No comments:

Post a Comment