-2

The user has 10 minutes to complete this quiz and it should terminate when 10 minutes(600 seconds) are over.

Currently the code to my knowledge starts a timer, prints a question, receives an answer and checks if the answer is correct, and after that, checks to see the time elapsed.

From debugging, it would seem that as long as I have not answered a question, the time continues to elapse past 10 minutes, where I want the program to terminate as soon as 10 minutes is reached. I dont know if that is possible.

I would prefer if the code would immediately terminate when 600 seconds end. Something in the nature of while (time > 600 sec) { execute code } I don't know if thats possible or what functions could assist in that.

    srand(time(0));
    int score = 0;
    int QAnswered = 0;

    //initializes 10 minute timer and tells user how long they have.

    auto start = high_resolution_clock::now();
    auto end = high_resolution_clock::now();
    auto duration = duration_cast<seconds>(end - start);
    cout << "Time Limit is 10 minutes\n" << endl;

    //loops through all 10 questions
    for (int k = 0; k < 10; k++) {
        int y = rand() % 100;  //random number 1-100 to pick a question out 
        
        cout << "Q." << k + 1 << " " << Q[y] << y << endl;  //prints Question number and then Question
        cout << "Please enter your answer" << endl;;
        cin >> ans;
        transform(ans.begin(), ans.end(), ans.begin(), ::toupper);
        if ((ans == "T" || ans == "TRUE") && A[y] == "TRUE")
            score++;
        if ((ans == "F" || ans == "FALSE") && A[y] == "FALSE")
                score++;
            
        end = high_resolution_clock::now();
        duration = duration_cast<seconds>(end - start);
        if (duration.count() >= 600) {
            cout << "Time Finished" << endl;
            break;
        }
    }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    please post a [mcve]. Your code is incomplete, but I think you can remove most of it while still demonstrating the same problem. What is the problem btw? Does the code not terminate after 10 minutes? – 463035818_is_not_an_ai Sep 22 '20 at 12:00
  • There are no standard ways to abort input from `std::cin`, but there might be operating system dependent ways to do it. You also need to have a thread running in the background that does the timing, and which aborts the input using the OS-specific ways available. – Some programmer dude Sep 22 '20 at 12:01
  • 2
    @Someprogrammerdude for simplicity I would simply give the user all the time they need to type the answer and check for timeout afterwards, not sure but I think thats OPs approach – 463035818_is_not_an_ai Sep 22 '20 at 12:11
  • @idclev463035818 Well that's a possibility, and certainly simpler. :) – Some programmer dude Sep 22 '20 at 12:25
  • I attempted to remove all the excess code that has nothing to do with using timers in C++ – Stephen Simpson Sep 22 '20 at 13:12
  • 1
    you removed a bit too much, there is no `cin` anymore in the code. Also it would be better if you explain what makes you think that your code in incorrect. What does it do? How does that differ from what it should do? – 463035818_is_not_an_ai Sep 22 '20 at 14:06
  • i see no problem with the code you did post. Note that there are at least two strategies of how to implement such timeout (see comments above). We dont know what exactly you want. – 463035818_is_not_an_ai Sep 22 '20 at 14:07
  • I added the basic code without all the unnecessary test cases. Currently the code to my knowledge starts a timer, prints a question, receives an answer and checks if the answer is correct, and after that, checks to see the time elapsed. From debugging, it would seem that as long as I have not answered a question, the time continues to elapse past 10 minutes, where I want the program to terminate as soon as 10 minutes is reached. I dont know if that is possible. – Stephen Simpson Sep 22 '20 at 14:19

1 Answers1

-3

I am not sure this is a proper c++ question but rather something in the domain of design.

There is nothing wrong with the way you are calculating time as far as I can see, but I would go with the design proposed above, and have a different thread perform the countdown, and when countdown is finished kill the "question asking" thread.

I would use thread from boost library, as they can be forcibly terminated, and it seems this is needed since we might be waiting for user response when the time is up.

Good luck!

Yitshak Yarom
  • 84
  • 1
  • 4