Friday, December 9, 2016

Programming Challenge 5.8 - Math Tutor V3

/* Math Tutor V3 - This program is the third version of Math Tutor. It is modified
   so that it displays a menu allowing the user to select an addition, subtraction,
   multiplication, or division problem. The final selection on the menu lets the
   user quit the program. After the user has finished the math problem, the program
   displays the menu again. This process is repeated until the user chooses to quit
   the program.
 
   Input Validation: If the user selects an item not on the menu, an error message
   is displayed, and the menu is displayed again. */

#include "Utility.h"

int main()
{
    /* Constants: Minimum, Maximum */
    const int MIN_ADDITION_SUBTRACTION = 1;
    const int MAX_ADDITION_SUBTRACTION = 500;
    const int MIN_MULTIPLICATION_DIVISION = 1;
    const int MAX_MULTIPLICATION_DIVISION = 100;

    /* Constants: Menu items */
    const int ADDITION = 1,
              SUBTRACTION = 2,
              MULTIPLICATION = 3,
              DIVISION = 4,
              QUIT = 5;

    /* Variable: Menu item, Number one, Number Two, Answer, Student answer */
    int menuItem = 0,
        numOne = 0,
        numTwo = 0,
        answer = 0,
        studentAnswer = 0;

    /* Do While loop::Display: Menu */
    do
    {
        cout << "\t\tWelcome to your personal Math Tutor: Fun with Math!\n"
            << "\nPlease select an item from the following menu to begin\n"
            << "todays training session.\n"
            << "1. Addition\n"
            << "2. Subtraction\n"
            << "3. Multiplication\n"
            << "4. Division\n"
            << "5. Quit.\n"
            << "Enter your choice (1 through 5): ";
        cin >> menuItem;

        /* While loop::Input Validation: While the user selects an item
           outside the range of 1 through 5, the user is asked to enter
           a valid number */
        while (menuItem < ADDITION || menuItem > QUIT)
        {
            /* Display: Error message */
            cout << "\nLittle Math Warrior, you have selected a menu item that\n"
                << "does not exist. Please select items 1 through 4 to start\n"
                << "your training, or choose 5 to Quit\n";

            /* Ask the user again */
            cout << "Enter your choice: ";
            cin >> menuItem;
        }

        /* Seed the random number generator */
        srand((unsigned int)time(NULL));

        numOne = (rand() % (MAX_ADDITION_SUBTRACTION - MIN_ADDITION_SUBTRACTION + 1))
            + MIN_ADDITION_SUBTRACTION;
        numTwo = (rand() % (MAX_ADDITION_SUBTRACTION - MIN_ADDITION_SUBTRACTION + 1))
            + MIN_ADDITION_SUBTRACTION;
        numOne = (rand() % (MAX_MULTIPLICATION_DIVISION - MIN_MULTIPLICATION_DIVISION + 1))
            + MIN_MULTIPLICATION_DIVISION;
        numTwo = (rand() % (MAX_MULTIPLICATION_DIVISION - MIN_MULTIPLICATION_DIVISION + 1))
            + MIN_MULTIPLICATION_DIVISION;

        /* Switch Case */
        switch (menuItem)
        {
        case ADDITION:
            /* Display: Math problem */
            cout << "\n" << setw(4) << right << numOne << endl;
            cout << '+' << setw(3) << right << numTwo << endl;
            cout << "----\n";

            /* Answer */
            answer = numOne + numTwo;

            /* Ask the student to enter his or her answer */
            cout << "\nEnter your answer: ";
            cin >> studentAnswer;
            break;

        case SUBTRACTION:
            /* Display: Math problem */
            cout << "\n" << setw(4) << right << numOne << endl;
            cout << '-' << setw(3) << right << numTwo << endl;
            cout << "----\n";

            /* Answer */
            answer = numOne - numTwo;

            /* Ask the student to enter his or her answer */
            cout << "\nEnter your answer: ";
            cin >> studentAnswer;
            break;

        case MULTIPLICATION:
            /* Display: Math problem */
            cout << "\n" << setw(4) << right << numOne << endl;
            cout << '*' << setw(3) << right << numTwo << endl;
            cout << "----\n";

            /* Answer */
            answer = numOne * numTwo;

            /* Ask the student to enter his or her answer */
            cout << "\nEnter your answer: ";
            cin >> studentAnswer;
            break;

        case DIVISION:
            /* Display: Math problem */
            cout << setw(4) << right << numOne << endl;
            cout << '/' << setw(3) << right << numTwo << endl;
            cout << "----\n";

            /* Answer */
            answer = numOne / numTwo;

            /* Ask for the student to enter his or her answer */
            cout << "\nEnter your answer: ";
            cin >> studentAnswer;
            break;

        case QUIT:
            cout << "\nEveryone needs a break to grow strong, especially you, little\n"
                << "math warrior! Come back and try again to strengthen your skills.\n";
            break;
        }

            /* While Loop::Check - While the student's answer is incorrect,
            the student is asked to enter the answer again. */
            while (studentAnswer != answer)
            {
                cout << "\nSorry, little math warrior, this was not the correct\n"
                    << "answer to this problem.\n\n";

                /* Display: Ask the user to try again */
                cout << "Try again: ";
                cin >> studentAnswer;
            }

            /* If Statement::Display: If the answer was correct, and menu
               choice is not QUIT, a congratulating message and the correct
               answer is displayed */
            if (studentAnswer == answer && menuItem != QUIT)
            {
                if (menuItem == ADDITION)
                {
                    cout << "\n" << setw(4) << right << numOne << endl;
                    cout << '+' << setw(3) << right << numTwo << endl;
                    cout << "----\n";
                    cout << setw(4) << right << studentAnswer << endl;
                }

                else if (menuItem == SUBTRACTION)
                {
                    cout << "\n" << setw(4) << right << numOne << endl;
                    cout << '-' << setw(3) << right << numTwo << endl;
                    cout << "----\n";
                    cout << setw(4) << right << studentAnswer << endl;
                }

                else if (menuItem == MULTIPLICATION)
                {
                    cout << "\n" << setw(4) << right << numOne << endl;
                    cout << '*' << setw(3) << right << numTwo << endl;
                    cout << "----\n";
                    cout << setw(4) << right << studentAnswer << endl;
                }

                else if (menuItem == DIVISION)
                {
                    cout << "\n" << setw(4) << right << numOne << endl;
                    cout << '/' << setw(3) << right << numTwo << endl;
                    cout << "----\n";
                    cout << setw(4) << right << studentAnswer << endl;
                }

                cout << "\nCongratulations, little math warrior!\n"
                    << studentAnswer << " was the correct answer!\n"
                    << "Why don't you try another problem?\n\n";
            }
    } while (menuItem != QUIT);

    pauseSystem();
    return 0;
}

No comments:

Post a Comment