0

I don't have any code for it but supposed the user should answer the math questions in 2 mins time and generate it's score:

sample output1:

1+2 = *user's answer*
1+5 = *user's answer*
5+6 = *user's answer*
9+0 = *user's answer*
5+8 = *user's answer*
2+9 = *user's answer*

Time: 0:2:0:0                                                                                 
Time's up!                                                                                   
Your score: 6 

sample output2:

1+2 = *user's answer*
1+5 = *user's answer*
5+6 = *user's answer*
9+0 = *user's answer*


Time: 0:2:0:0                                                                                
Time's up!                                                                                  
Your score: 4 

code:

void add()  //Addition
{
    int x,num,num2,ans,sum,score=0;
    time_t t;

    clrscr();

    for(x=0;x<5;x++) // I tried for(x=0;x<=time();x++) but compiled error
    {
        srand((unsigned) time(&t));
        num=rand()%9;
        num2=rand()%9;
        sum=num+num2;

        gotoxy(30,14);
        clrscr();
        printf("\n%d + %d = ",num,num2);
        printf("\nAns: ");
        scanf("%d",&ans);

        if(ans==sum)
        {
            score++;
        }
        else
        {
        printf("\nengggk!!!\n");
        }

    }

    printf("\n\t\t\t ....Your score: %d",score);
    pass(score);
}

void time()
{
    int h,m,s;

    clrscr();
    for(h=0;h<12;h++)
    {
        for(m=0;m<60;m++)
        {
            for(s=0;s<60;s++)
            {
                 printf("TIMER::\n%d:%d:%d",h,m,s);

             if(m==2)
             {
                     printf("Time's up!");
                     pass();
                 return m;
                 }
             }
         }

}

I really don't know how to do it so, please help me.

Thank you.

Dan
  • 10,531
  • 2
  • 36
  • 55
Hasmine
  • 67
  • 5
  • 4
    Please indent your code properly. – Tony The Lion Aug 21 '13 at 14:52
  • ok :) I'm sorry, I'm just new to this – Hasmine Aug 21 '13 at 14:57
  • 2
    From the looks of your code, you are using an extremely old compiler (probably Turbo C or similar). You will have much better results if you use something more recent. –  Aug 21 '13 at 15:02
  • 1
    possible duplicate of [How to deactivate input statement after some time?](http://stackoverflow.com/questions/18289635/how-to-deactivate-input-statement-after-some-time) – alk Aug 21 '13 at 15:05
  • yes, it's what my professor required us to use – Hasmine Aug 21 '13 at 15:05
  • Put bluntly: your professor is a moron. I feel for you. – abelenky Aug 21 '13 at 15:22
  • Majority voting has its problems — it is very close to a duplicate of 'How to deactive input statement after some time'. I don't regard it as "unclear what you're asking", but other people did. – Jonathan Leffler Aug 21 '13 at 15:25
  • why? does she making it hard for us? (sorry for my grammar, I'm a Filipina) – Hasmine Aug 21 '13 at 15:26
  • Turbo C was introduced in 1987, when 286 processors were considered "new". I don't think it has been substantially updated since 1990. I don't think it can support 32-bit addressing, or many other important modern aspects. There are far better, free, modern compilers available. Its not terribly relevant to your question, but your Prof. should really consider modern alternatives to a 20+ year old compiler. – abelenky Aug 21 '13 at 15:37
  • ah, I see. I prefer to use object-oriented languages. – Hasmine Aug 21 '13 at 15:40
  • @abelenky: Perhaps they are running your old 286 over there? – alk Aug 21 '13 at 16:57
  • @alk: If so, I hope they enjoy all the 640 x 480, 1-bit pics of centerfolds. :) – abelenky Aug 21 '13 at 17:31

3 Answers3

2

Assuming a sufficiently POSIX-like system, you can use alarm() with either signal() or (better) sigaction(). In outline:

static volatile sig_atomic_t alarmed = 0;

static void alarm_handler(int signum)
{
    signal(signum, alarm_handler);  // Uses signum — probably not strictly necessary
    alarmed = 1;
}

...

signal(SIGALRM, alarm_handler);
alarm(2*60);

if ((nbytes = read(line, sizeof(line), STDIN_FILENO)) > 0)
{
    ...process normal answer...
}
else if (nbytes == 0)
    ...process EOF...
else if (errno == EINTR)
    ...check whether alarm went off — alarm_handler should set a flag...
else
    ...something broke — report error...

Given that you're probably using Standard I/O, you change the read() to fgets() and check for EOF with feof() and you can assume that you got an error without troubling ferror() and do the errno check. You might prefer to set errno to 0 before calling the I/O function; it isn't strictly necessary, though.

You can cancel the alarm call at any point you wish by calling alarm(0);.


I went digging in a program of mine called timeout which runs another command and waits for it to finish — but times it out if it doesn't finish quickly enough. There are some notes that might help you in the comments:

/*
** On MacOS X, the signal() function is implemented using sigaction()
** and includes SA_RESTART in the options, so that when the alarm times
** out, the signal catcher is called but wait() resumes, which is
** not what's wanted.  Hence, upgrade to use sigaction() directly (when
** available; assume it is available by default) without the SA_RESTART.
*/
static void set_interruptible_alarm_handler(void (*handler)(int))
{
#ifdef NO_SIGACTION
    /* Unlikely to be necessary on modern POSIX systems */
    signal(SIGALRM, catcher);
#else
    struct sigaction act;

    if (sigemptyset(&act.sa_mask) != 0)
        err_syserr("sigemptyset failed: ");
    act.sa_handler = handler;
    act.sa_flags = 0;
    if (sigaction(SIGALRM, &act, 0) != 0)
        err_syserr("failed to set signal handler\n");
#endif /* NO_SIGACTION */
}

Using sigaction() is more verbose than signal(), but the control is better.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

You can't with the standard facilities. The input functions block until you receive input and only then can you execute code again. So you have two options:

  1. Take the time before you wait for input. When you've received input, take the time again, and if the user took too long, reject the answer.

  2. Use a different input system. For example, you can do it using Boost.Asio, although it's not exactly trivial.

https://github.com/rtsuk/liaw2013/wiki/5.3-how-to-time-the-user

unwind
  • 391,730
  • 64
  • 469
  • 606
Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
0

As you've said that you're using Turbo C, you can use kbhit().
Documentation Here

Its a non-portable function, but in Turbo C world, it will return a recently pressed key, or zero.

So you can put it in a loop that checks the current time, and if a key is hit.

abelenky
  • 63,815
  • 23
  • 109
  • 159