i need a code for takin input fo 2 secs

Hi i am bilal n i am new here
i need to make a program on Human simulation.......
Well what i need is a code. when i am executing a program like am printing 'x' for infinite many times during execution when the console is printing x infinitely what can i do to stop that and 1 more
i also need a statement that take input from user bu without waiting for user to input or if the user inputs nuffin in 2 secs the program shall proceed ahead .....................
PLZ PLZ PLZ PLZ PLZ PLZ help me i really need buth of those or atleast the latter one..
Well i think may be i didn't explained my question clearly
Here it goes again
I need a command or function for which user gets only 2 secs to input a value if he doesn't input any thing in that 2 secs the program proceeds.. In actual the problem is when we want an input form user the execution pauses until or unless the user press any thing from keyboard. But what i need is that program only stops for 2 secs if no one inputs any thing than the program move forward.....
Well, you cant use cin or getline or someting, because those commands waits for input.

If you're programming on windows, you can use GetAsyncKeyState(...), wich only checks the state of a certain key and doesnt wait for input. You could place this command in a loop of a wait function (include time.h). For example, if you want to give the user two secconds to quit the program, using escape, you could use this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <time.h>
#include <windows.h>

using namespace std;

bool end=false;

void waitToQuit (int time)
{
     clock_t endWait;
     bool noInput=true;
     endWait=clock()+time;
     while (clock()<=endWait && noInput)
     {
           if (GetAsyncKeyState(VK_ESCAPE))
           {
              end=true;
              noInput=false;
           }
     }
}

int main()
{
    int count=1;
    while (!end)
    {
       cout<<count<<"\n";
       waitToQuit(2000);
       count++;
    }
    return 0;
} 


Hope this helps
Fanx bu this wasn't helpin. i want to make a assignment it goes like

The program should have the functionality to simulate human life e-g a person’s health, emotions, hunger etc should be monitored and incase anything is lower then the required level, the program should alarm and notify about that thing. And also provide a means to fulfill the requirement.
e-g if a person’s hunger is below the least required point, the program will alarm and notify and will also require proteins as an input to recover the hunger meter. The output must be updated constantly and the input can be given any time :S .

I really want to do it ma self. So if i can get the statements codes and their syntaxes that would be really helpful.

I am looking forward to get help from this forum.
Use getch() from conio.h and use a loop:
1
2
3
4
5
6
7
int keyinput;
while (true)
{
    keyinput=0;
    keyinput=getch();
    //...
}
Oh god - i can't stand PLZPLZPLZPLZPZLZPLZPLPLZPZLPZLL
Argh. Stay away from getch() and GetAsyncKeyState() as much as possible (but prefer the former to the latter in cross-platform code).

What you are asking about is called "unbuffered input". By default, the standard input stream is "line buffered", meaning that your program only gets input when the user presses the Enter key.

To change that, you have to do platform-specific things.

If you are on Windows, you don't actually have to turn off line buffering just to check to see if there is input waiting to be read. In Unix/Linux you do.

See http://www.cplusplus.com/forum/general/5304/page1.html#msg23940

Here's a little Windows program to demonstrate how-to in Windows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace std;

#include <windows.h>

bool iskeypressed( unsigned timeout_ms = 0 )
  {
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    timeout_ms
    ) == WAIT_OBJECT_0;
  }

int main()
  {
  string name;
  cout << "Please enter your name.";
  while (!iskeypressed( 500 ))
    {
    cout << "." << flush;
    }
  getline( cin, name );

  cout << "Thank you, " << name << endl;
  return 0;
  }

[edit] Oh, yeah, for two seconds specify 2000 ms in the call to iskeypressed(). It only waits the full two seconds if no input is available.[/edit]

Hope this helps.
Last edited on
And here's a sample program that demonstrates how to set unbuffered stdin on Un*x. You'll need to modify it slightly for your application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int main()
{
    struct termios oldSettings, newSettings;

    tcgetattr( fileno( stdin ), &oldSettings );
    newSettings = oldSettings;
    newSettings.c_lflag &= (~ICANON & ~ECHO);
    tcsetattr( fileno( stdin ), TCSANOW, &newSettings );    

    while ( 1 )
    {
        fd_set set;
        struct timeval tv;

        tv.tv_sec = 10;
        tv.tv_usec = 0;

        FD_ZERO( &set );
        FD_SET( fileno( stdin ), &set );

        int res = select( fileno( stdin )+1, &set, NULL, NULL, &tv );

        if( res > 0 )
        {
            char c;
            printf( "Input available\n" );
            read( fileno( stdin ), &c, 1 );
        }
        else if( res < 0 )
        {
            perror( "select error" );
            break;
        }
        else
        {
            printf( "Select timeout\n" );
        }
    }

    tcsetattr( fileno( stdin ), TCSANOW, &oldSettings );
    return 0;
}

Thanx guyz! This really helps
well but i now get another command of conio.h i.e kbhit();

Topic archived. No new replies allowed.