Sunday, November 20, 2016

Programming Challenge 4.11 - Math Tutor Improved

/* Math Tutor Improved - This program is an improved version written for Programming
   Challenge 3.17. It can be used as a math tutor for a young student. The program
   displays two random numbers that are to be added, such as:
  
      247
     +129
     ----
   
   The program waits for the student to answer. If the answer is correct, a message
   of congratulations is displayed. If the answer is incorrect, a message is printed
   showing the correct answer. */

#include "Utility.h"

int main()
{
    /* Constants for minimum and maximum range of numbers */
    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 for menu choices */
    const int ADDITION = 1,
              SUBTRACTION = 2,
              MULTIPLICATION = 3,
              DIVISION = 4,
              QUIT = 5;

    /* Hold two numbers, the answer, and menu choice */
    int numOne = 0,
        numTwo = 0,
        answer = 0,
        choice = 0,
        studentAnswer = 0;

    /* Random seed (the way it is initialized error t_time is caught */
    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;

    /* Display the menu and get the students choice */
    cout << "\t\tWelcome to Math Tutor!\n\n"
         << "1. Addition\n"
         << "2. Subtraction\n"
         << "3. Multiplication\n"
         << "4. Division\n"
         << "5. Quit\n"
         << "\nEnter your choice: ";
    cin >> choice;

    /* Respond to the students menu selection and formatting the output */
    cout << fixed << showpoint << setprecision(2);

    switch (choice)
    {           
    case ADDITION:
        cout << "\n" << setw(4) << right << numOne << endl;
        cout << '+' << setw(3) << right << numTwo << endl;
        cout << "----\n";
   
        /* Do the calculations */
        answer = numOne + numTwo;

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

        /* Show the answer */
        cout << "\n" << setw(4) << right << numOne << endl;
        cout << '+' << setw(3) << right << numTwo << endl;
        cout << "----\n";
        cout << setw(4) << right << studentAnswer << endl;

        /* Check whether the students answer is correct and display the result */
        (studentAnswer != answer) ?
            cout << "\nSorry, this was the wrong answer.\n"
            << setw(4) << right << "The correct answer is: "
            << answer << endl :
            cout << "\nCongratulations, this is the right answer!\n"
            << "Why don't you try another problem?\n";
        break;
   
    case SUBTRACTION:
        cout << "\n" << setw(4) << right << numOne << endl;
        cout << '-' << setw(3) << right << numTwo << endl;
        cout << "----\n";

        /* Do the calculations */
        answer = numOne - numTwo;

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

        /* Show the answer */
        cout << "\n" << setw(4) << right << numOne << endl;
        cout << '-' << setw(3) << right << numTwo << endl;
        cout << "----\n";
        cout << setw(4) << right << studentAnswer << endl;

        /* Check whether the students answer is correct and display the result */
        (studentAnswer != answer) ?
            cout << "\nSorry, this was the wrong answer.\n"
            << setw(4) << right << "The correct answer is: "
            << answer << endl :
            cout << "\nCongratulations, this is the right answer!\n"
            << "Why don't you try another problem?\n";
        break;

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

        /* Do the calculations */
        answer = numOne * numTwo;

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

        /* Show the answer */
        cout << "\n" << setw(4) << right << numOne << endl;
        cout << '*' << setw(3) << right << numTwo << endl;
        cout << "----\n";
        cout << setw(4) << right << studentAnswer << endl;

        /* Check whether the students answer is correct and display the result */
        (studentAnswer != answer) ?
            cout << "\nSorry, this was the wrong answer.\n"
            << setw(4) << right << "The correct answer is: "
            << answer << endl :
            cout << "\nCongratulations, this is the right answer!\n"
            << "Why don't you try another problem?\n";
        break;

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

        /* Do the calculations */
        answer = numOne % numTwo;

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

        /* Show the answer */
        cout << "\n" << setw(4) << right << numOne << endl;
        cout << '/' << setw(3) << right << numTwo << endl;
        cout << "----\n";
        cout << setw(4) << right << studentAnswer << endl;

        /* Check whether the students answer is correct and display the result */
        (studentAnswer != answer) ?
            cout << "\nSorry, this was the wrong answer.\n"
            << setw(4) << right << "The correct answer is: "
            << answer << endl :
            cout << "\nCongratulations, this is the right answer!\n"
            << "Why don't you try another problem?\n";
        break;
       
    case QUIT:
        cout << "Everyone needs a break after studying hard! Try again soon!\n";
        break;

    default: cout << "Please choose between 1 and 5.\n";
        break;
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment