0

I am trying to add a countdown timer to this program. I would like the timer to start when the first math fact question is asked and upon expiration i want the program to give the grade. What's the code to do this in c++ if possible?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

int main(int args, char* argv[])
{
    int i;
    int result;
    int solution;
    char fact;
    bool done = false;
    int correct = 0;
    int count = 0;

    do {
        try {
            cout << "Enter (m)ultiplication or "
                 << "(a)ddition." << endl;  /*or (s)ubstraction. */
            cin >> fact;

            while (!cin)
                throw fact;

            if (fact != 'A')
                if (fact != 'a')
                    if (fact != 'M')
                        if (fact != 'm')

                            while (!cin)
                                throw fact;
            cout << "Now, enter the number of the fact that
                 you would like to do." << endl;
            cin >> i;

            int wrong = 0;
            int score = 0;
            int j = 0;

            while (!cin)
                throw i;

            switch (fact) {
            case 'm':
            case 'M':

                while (j < 13) {

                    cout << "What's " << i << " x " << j << "?" << endl;
                    cin >> result;

                    while (!cin)
                        throw result;

                    solution = i * j;
                    if (result == solution) {
                        cout << "Great Job!  That is the correct answer for the problem "
                             << i << " x " << j << "." << endl;
                        cout << endl;
                        cout << endl;
                        cout << endl;
                        score++;
                        j++;
                        cout << endl;
                    }
                    if (result != solution) {
                        cout << "Oh no!  " << result << " is NOT the correct answer for "
                             << i << " x " << j << "." << endl;

                        wrong = wrong + 1;
                        count++;
                    }
                    if (count == 3) {
                        cout << "The correct answer is " << i * j << "." << endl;

                        j++;
                        wrong = wrong - 3;
                        count = 0;

                    }
                    if (count == 1) {
                        cout << endl;
                        count--;
                        wrong = wrong - 1;
                    }
                    if (count == 2) {
                        cout << endl;
                        count--;
                        wrong = wrong - 2;
                    }
                }
            case 'a':
            case 'A':

                while (j < 13) {

                    cout << "What's " << i << " + " << j << "?" << endl;

                    cin >> result;

                    while (!cin)
                        throw result;

                    solution = i + j;
                    if (result == solution) {
                        cout << "Great Job!  That is the correct answer for the problem "
                             << i << " + " << j << "." << endl;
                        cout << endl;
                        cout << endl;
                        cout << endl;
                        score++;
                        j++;
                        cout << endl;
                    }
                    if (result != solution) {
                        cout << "Oh no!  " << result << " is NOT the correct answer for "
                             << i << " + " << j << "." << endl;

                        wrong = wrong + 1;
                        count++;
                    }
                    if (count == 3) {
                        cout << "The correct answer is " << i + j << "." << endl;

                        j++;
                        wrong = wrong - 3;
                        count = 0;

                    }
                    if (count == 1) {
                        cout << endl;
                        count--;
                        wrong = wrong - 1;
                    }
                    if (count == 2) {
                        cout << endl;
                        count--;
                        wrong = wrong - 2;
                    }
                }
                if (j == 13) {
                    system("pause");

                    correct = score - wrong;
                    score = (correct * 100) / 13;
                }
                if (score >= 80) {
                    cout << "Excellent!!!!!" << endl;
                    cout << "You scored " << score << "%." << endl;
                    cout << "You got " << correct << " out of 13 correct." << endl;
                    cout << "Keep up the good work." << endl;
                } else if (score >= 70) {
                    cout << "Congratulations!!!!!" << endl
                         cout << "You scored " << score << "%." << endl;
                    cout << "You got " << correct << " out of 13 correct." << endl;
                    cout << "Let's see if we can score even higher next time." << endl;
                } else {
                    cout << "You scored below 70 which means that you may need some"
                         << " more practice." << endl;
                    cout << "You scored " << score << "%." << endl;
                    cout << "You got " << correct << " out of 13 correct." << endl;
                    cout << "You might want to try the " << i << " facts again."
                         << "  Goodluck!!!!!" << endl;
                }
            }
        } catch (char fact) {
            cout << "Invalid input.  You can only enter (m)ultiplication or"
                 << " (a)ddition.  Please try again." << endl;
            cin.clear();
            cin.ignore(100, '\n');
        } catch (int i) {
            cout << "Invalid input0.  You can only enter a
                 number here.  Please try again." << endl;
            cin.clear();
            cin.ignore(100, '\n');
        } catch (...) {
            cout << "Invalid input2.  You can only enter a number here.
                 Please try again." << endl;
            cin.clear();
            cin.ignore(100, '\n');
        }
    } while (!done);
    return 0;
}
marcelovca90
  • 2,673
  • 3
  • 27
  • 34
cyruking
  • 3
  • 4

1 Answers1

1

The task is quite hard, but if you dare trying, I suggest doing it in two steps:

Implement inaccurate solution: timer expiration is checked between queries to user.

If there is some time left, next question is asked, otherwise statistics is shown. So program always waits for user input on the last question despite timer has run out. Not what exactly quizzes look like, but good move to start with.

Method: before starting quiz save current time, before each question take delta between saved time and current one and compare with time limit. Example with chrono (starting from C++11), example with oldschool clock

Add middle-question interruption

This part requires function, which will wait for user input not longer, than specified amount of time. So instead of using std::cin() you'll need to calculate amount of time left (time limit minus delta between cur time and start time) and call some sort of cin_with_timeout(time_left).

The hardest thing is implementing cin_with_timeout(), which requires solid knowledge of multithreading and thread synchronization. Great inspiration can be found here, but it is direction to start thinking rather than complete solution.

Community
  • 1
  • 1
nnovich-OK
  • 2,900
  • 2
  • 13
  • 16