1

I am writing a simple program to help my niece learn her multiplication tables. To prevent her from taking too long, I want to use a timer which, after a set amount of time passed, will cause the program to skip to the next question automatically without her inputting anything. If the program then does this, it will increment a counter which shows how many times she ran out of time, something like this:

if(table_to_test == 2)
       {
           finish = clock() + time_to_complete;
           ++tries;
           printf("2 x 8 = ");
           scanf("%d", &answer);
           if(answer == 16 && (clock() < finish))
           {
               printf("Correct!\n");
               ++amount_correct;
           }
           else if(answer != 16 && (clock() < finish))
           {
               printf("\aWrong!\n");
           }
           else if(clock() > finish)
           {
               printf("\aTime's up!\n");
               ++ran_out_of_time;
           }
       }

finish is how long she has before the timer skips to the next question, and time_to_complete is what the user input's for how long the program gives you before automatically skipping to the next question.

Whenever I run this, no matter how quickly I enter the answer, the program outputs the "Time's up!" line.

time_to_complete and finish are of type int

ThE411
  • 33
  • 8
  • did you check unit of the time_to_complete? e.g. (second, millisecond, microsecond ) – tigris Jun 01 '16 at 08:23
  • Why not store the value returned by `clock()` in a new variable after the answer is entered and then use a debugger to track the value? – mathematician1975 Jun 01 '16 at 08:23
  • Side note: as [Man](http://man7.org/linux/man-pages/man3/clock.3.html) says clock function return `clock_t` type. I proposed the duplicate because `scanf` is blocking teh code until user input data. – LPs Jun 01 '16 at 08:35

2 Answers2

1

You can't do this with scanf, it's too blunt - it will lock up the program forever if the user doesn't type anything.

You will have to use some OS-specific, non-standard console function which allows you to peek at the console input without waiting for the user. Use this in combination with a timer that flags you when the time is up.

You don't mention which system you are using. There's Linux ncurses or Windows "console API" etc, none of them are standard C.

Professional solutions involve using multiple threads for this, but that might be a bit overkill here.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I'm using Windows, and my compiler is CodeBlocks. I don't necessarily need to use this code exactly as it is. Is there another way for me to implement some sort of timer like this? – ThE411 Jun 01 '16 at 09:48
  • @ThE411 Codeblocks is an IDE, not a compiler. I'm guessing you are using gcc/mingw. You'll want to look at Windows API `PeekConsoleInput`, [example here](http://stackoverflow.com/questions/9782287/undefined-reference-to-gotoxy-in-c/9783195#9783195). As for the timer, there are lots of different options, like [this](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644901%28v=vs.85%29.aspx). – Lundin Jun 01 '16 at 09:52
-1

Do you specify time_to_complete variable as time into seconds?

Try to change calls of clock() for calls of following function:

clock_t clock_wrapper()
{
   return clock()/CLOCKS_PER_SEC;
}

if(table_to_test == 2)
   {
       finish = clock_wrapper() + time_to_complete;
       ++tries;
       printf("2 x 8 = ");
       scanf("%d", &answer);
       if(answer == 16 && (clock_wrapper() < finish))
       {
           printf("Correct!\n");
           ++amount_correct;
       }
       else if(answer != 16 && (clock_wrapper() < finish))
       {
           printf("\aWrong!\n");
       }
       else if(clock_wrapper() > finish)
       {
           printf("\aTime's up!\n");
           ++ran_out_of_time;
       }
   }

CLOCKS_PER_SEC is divider for convert clocks to seconds.

  • Start program, run until `scanf`, go make coffee. You can't tell if it took too long before the user actually inputs something. – Lundin Jun 01 '16 at 08:31
  • I can't see requirements for sscanf interrupt from question text. If it needed in simple case we can use alarm() call and SIGTERM handler for this on POSIX-capatible OS. – Anton Oshchepko Jun 02 '16 at 04:09
  • "skip to the next question automatically without her inputting anything" – Lundin Jun 02 '16 at 07:30