Advertisement
frozenking

Timed User Input C

Dec 9th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. //imran rana
  2. #include <time.h>
  3. #include <iostream>
  4. #include <conio.h>
  5. #include<stdio.h>
  6.  
  7. using namespace std;
  8. char* get_timed_input(long timeout_seconds, char* input_buffer,int buffer_size)
  9. {
  10.     time_t start, now;
  11.     int nChars = 0;
  12.     int chr = 0;
  13.     const int VK_RETURN = 13;
  14.     // start timer
  15.     time(&start);
  16.     while( nChars < buffer_size && chr != VK_RETURN)
  17.     {
  18.  
  19.         time(&now);
  20.         if( (now-start) >= timeout_seconds)
  21.         {
  22.             // timeout
  23.             input_buffer[nChars] ='\0';
  24.             return NULL;
  25.         }
  26.         while( !_kbhit() )
  27.         {
  28.             time(&now);
  29.             if( (now-start) >= timeout_seconds)
  30.             {
  31.                 // timeout
  32.                 input_buffer[nChars] = '\0';
  33.                 return NULL;
  34.             }
  35.         }
  36.         chr = getche();
  37.         input_buffer[nChars++] = chr;
  38.     }
  39.  
  40.     input_buffer[nChars] = '\0';
  41.     return input_buffer;
  42.  
  43. }
  44. int main()
  45. {
  46.     char imran[500]= {0};
  47.     get_timed_input(3,imran,500);
  48.     printf("\n:) User Input : %s",imran);
  49.     getch();
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement