--- In [email protected], "remarknibor" <[EMAIL PROTECTED]> wrote:
>
> Hi.
>
> I was wondering how to have a timer running while accepting a user's
> input. Specifically, I am having someone write an essay and I'm saving
> it to a variable when it's done using 'getline(cin,essay)'. However,
> while the person is typing their essay, nothing else in the programme
> is happening, and I'd actually like to be able to time their writing
> and stop them when 10 minutes has passed.
>
> Any ideas as to how I could achieve this?
>
> Thanks in advance for the help,
>
> Robin.

Try this for a starter Robin:

// TimedEntry.cpp :
// Demo solution of timed entry with filters

#include <stdafx.h>
#include <stdio.h>
#include <conio.h>
#include <sys/timeb.h>
#include <time.h>

#define MAXINPUTLEN 256

int TimedEntry( char *strBuffer, int iMaxLen, int iTimer);
int IsKeypressValid(int ch);

char strInput[256];
int _tmain(int argc, _TCHAR* argv[])
{
   int iError;
   printf("What is 2+2? : ");
   iError = TimedEntry( strInput, MAXINPUTLEN, 15);

   switch(iError)
   {
     case 0:
       printf("\n\nYour Answer is:%s\n", strInput);
       break;

     case -1:
       printf("\n\nAllacated time for question expired\n");
       break;

     default:
       printf("\n\nUnknown error\n");
       break;
   }
   printf("\nTest is over\b\nHit AnyKey (if you can find it)");
   getch();
   return 0;
}

int TimedEntry( char *strBuffer, int iMaxLen, int iTimer)
{
   int iPos=0, ch;
   __int64 tTimer;
   struct __timeb64 timebuffer;
   _ftime64( &timebuffer );

   tTimer = timebuffer.time + iTimer;
   do
   {
     while( ! _kbhit() )
     {
        _ftime64( &timebuffer );
       if( tTimer < timebuffer.time)
         return(-1);
     }
     ch = getch();
     if( iPos < iMaxLen )
     {
       switch(ch)
       {
         case 8:
           if(iPos > 0)
           {
             printf("%c %c", ch, ch);
             iPos--;
           }
           break;

         case '\r':
         case '\n':
           strBuffer[iPos++] = (char)'\0';
           break;

         //
         // Put extra case: code in here to
         // filter out any unwanted charactors
         //

         default:
           if(IsKeypressValid(ch))
           {
             strBuffer[iPos++] = (char)ch;
             printf("%c", (char)ch);
           }
           break;
       }
     }
   } while(1);
   return(0);
}

int IsKeypressValid(int ch)
{
   int i;
   char strKeysAllowed[] =
"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

   for(i=0; i<strlen(strKeysAllowed); i++)
   {
     if( (char)ch == strKeysAllowed[i])
     return 1;
   }
   return 0;
}






[Non-text portions of this message have been removed]

Reply via email to