I need to make a program that takes one character as an input and instantly continues execution and the time limit for the user to enter that character is 2 or 3 seconds. please help me and if possible ppost an example. Thanks for your help.

I need to make a program that takes one character as an input and instantly continues execution and the time limit for the user to enter that character is 2 or 3 seconds. please help me and if possible ppost an example. Thanks for your help.

Please read this. There are plenty of people here who will help you solve problems with your code, but we do not want to do it for you. You have to try on your own first.

Try googling ctime for starters.

Please read this. There are plenty of people here who will help you solve problems with your code, but we do not want to do it for you. You have to try on your own first.

Try googling ctime for starters.

the thing is i'm not looking for the part of the code i need. I am looking for an example cause i'm a noob. i want to learn what to use. the 'how' is my problem and i'm not asking anyone to solve it for me. I'm asking to be pointed to the right direction

It also depends on the compiler you are using. Only a few compilers can really do what you want.

It also depends on the compiler you are using. Only a few compilers can really do what you want.

I am using Dev-cpp. Can you help me please?

Does Dev-cpp have the functions kbhit() and getch() ? If so, look up what they do. If not, I don't know if Dev can handle it.

Does Dev-cpp have the functions kbhit() and getch() ? If so, look up what they do. If not, I don't know if Dev can handle it.

Thanks for your help. I really appreciate it and it's been really helpful. Dev has getch() but the thing is that i need to have a time limit for the user to enter that character. Like 3 secs or something. How can I make the timer to work at the same time as getch and when three seconds pass the execution continues without the character?

You can't. That's why I asked about kbhit() . With that function (or one similar -- google) it can be done. That is as long as getch() is identical to Borland and Microsoft. I heard it wasn't at one point.

What you want to do is not something that is addressed by the C or C++ standards: you must use OS-specific code or special extensions.

On Windows (which you are using if you are using Dev-C++), you should use the WaitForSingleObject() function. Use GetStdHandle() to get the first argument. You'll need to #include <windows.h> .

On *nix systems you would want to use select() or poll() function.

Either way, you have the option of specifying a limited timeout. The timeout will always let you know whether the function returned because there is input waiting or the time just ran out, so you can choose how to continue based upon that.

Hope this helps.

What you want to do is not something that is addressed by the C or C++ standards: you must use OS-specific code or special extensions.

On Windows (which you are using if you are using Dev-C++), you should use the WaitForSingleObject() function. Use GetStdHandle() to get the first argument. You'll need to #include <windows.h> .

On *nix systems you would want to use select() or poll() function.

Either way, you have the option of specifying a limited timeout. The timeout will always let you know whether the function returned because there is input waiting or the time just ran out, so you can choose how to continue based upon that.

Hope this helps.

Thank you very much for taking interest in my problem. However i'm a newbie and i don't know what each function does exactly. Could you please help me by explaning how these functions work (the ones for windows)? Thanks again:)

#include <iostream>
#include <conio.h>

using namespace std;


bool Possible;

VOID CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
	printf( "\n3 Seconds Have Elapsed..." );
	Possible = false;
}


void main()
{
	SetTimer( 0, 1001, 3000, TimerProc ); // Set 3 Sec opening

	while( true )
	{
		static char Letter = NULL;

		if ( kbhit() && Possible ) // Get one letter
		{
			Letter = getch();
			Possible = false;
		}

		// Always run
		
	}
}

// kbhit() stands for Keyboard hit - Checks if a key has been pressed
// SetTimer() a api function that creates a windows message that will be sent to the Timer Procedure

you should be able to do your own research from here using msdn and google

No, don't bind the processor like that. Use one of the wait functions, like the one I suggested.

Here is a simple example:

#include <iostream>
#include <string>
using namespace std;

#include <windows.h>

int main()
  {
  string s;
  HANDLE hStdIn = GetStdHandle( STD_INPUT_HANDLE );

  cout << "I am going to count to 10.\n"
          "Press ENTER at any time to stop.\n";

  for (unsigned counter = 1; counter <= 10; counter++)
    {
    cout << "\r   \r" << counter << " " << flush;

    // Wait for one second on the standard input.
    // If standard input is waiting, we're done.
    if (WaitForSingleObject( hStdIn, 1000 ) == WAIT_OBJECT_0)
      {
      cout << "\r   \r> ";
      getline( cin, s );
      break;
      }
    }

  if (!s.empty())
    {
    cout << "(You entered \"" << s << "\".)\n";
    }

  cout << "OK. All done.\n";

  return 0;
  }

Try running it and letting the time run out.
Try running it and pressing ENTER.
Try running it and entering a string.

Hope this helps.

Will not work since you are using getline ( requires return ).

I need to make a program that takes one character as an input and instantly continues execution and the time limit for the user to enter that character is 2 or 3 seconds. please help me and if possible post an example. Thanks for your help.

if all you really need is a 3 second wait time to get a char then it doesn't take more than a

for( int i = 0; i < Seconds * 4; i++ )
{
   if( kbhit() )
  {
     var = getch();
     break;
  }
  Sleep( 250 );
}

nothing more, nothing less.

Edited 6 Years Ago by VilePlecenta: n/a

Will not work since you are using getline ( requires return ).

Argh, OK, I missed that non-linebuffered bit.

Nevertheless, rather than use a derelict library like <conio.h>, why not use the proper method? The Windows API works just fine.

#include <iostream>
using namespace std;

#include <windows.h>

//----------------------------------------------------------------------------
// Here is a little class that uses the Win32 functions to provide some
// nice, friendly input options.
//
struct key_t
  {
  enum mode_t { unbuffered, normal };

  HANDLE hStdIn;
  DWORD  initial_mode;

  //..........................................................................
  key_t()
    {
    hStdIn = GetStdHandle( STD_INPUT_HANDLE );
    if (!GetConsoleMode( hStdIn, &initial_mode ))
      {
      hStdIn = INVALID_HANDLE_VALUE;
      }
    else 
      {
      mode( unbuffered );
      }
    }

  //..........................................................................
  ~key_t()
    {
    if (isatty())
      {
      mode( normal );
      }
    }

  //..........................................................................
  bool isatty() const
    {
    return hStdIn != INVALID_HANDLE_VALUE;
    }

  //..........................................................................
  void mode( mode_t mode )
    {
    SetConsoleMode( hStdIn, (mode == unbuffered) ? 0 : initial_mode );
    }

  //..........................................................................
  bool ready( unsigned milliseconds = 0 )
    {
    return WaitForSingleObject( hStdIn, milliseconds ) == WAIT_OBJECT_0;
    }

  //..........................................................................
  int get()
    {
    INPUT_RECORD inrec;
    DWORD        count;
    ReadConsoleInput( hStdIn, &inrec, 1, &count );
    return inrec.Event.KeyEvent.wVirtualKeyCode;
    }

  };

//----------------------------------------------------------------------------
int main()
  {
  key_t key;
  int   ch = -1;

  //..........................................................................
  // Make sure the standard input is attached to a human being.
  //
  if (!key.isatty())
    {
    cout << "Hey foo! You gotta be a HUMAN to use this program!\n";
    return 1;
    }

  //..........................................................................
  // Get a key only if pressed within ten specific intervals.
  // 
  cout << "I am going to count to 10.\n"
          "Press any key at any time to stop.\n";

  for (unsigned counter = 1; counter <= 10; counter++)
    {
    cout << "\r   \r" << counter << " " << flush;

    if (key.ready( 1000 ))
      {
      ch = key.get();
      break;
      }
    }

  if (ch >= 0)
    {
    cout << "You pressed #" << ch << ".\n";
    }

  //..........................................................................
  // Get a string the normal way
  //
  key.mode( key_t::normal );
  cout << "Good job. What is your name? " << flush;
  string name;
  getline( cin, name );

  cout << "OK, " << name << "! All done.\n";

  //..........................................................................
  // Back to our "raw" and unbuffered input to do something oft-wanted.
  //
  key.mode( key_t::unbuffered );
  cout << "Press any key to quit." << flush;
  key.get();

  return 0;
  }

This rightly avoids <conio.h> functions like kbhit() and getch() (and uses the correct Win32 approach), and also skips Sleep() (which comes with <windows.h> anyway) in lieu of a more appropriate function -- one that terminates immediately when a key is pressed.


If you want something more cross-platform, I recommend you to the NCurses library (on Windows you'll want the PDCurses port).

If curses is the way you want to go, some required reading:
http://en.wikipedia.org/wiki/Ncurses#Programming_Introductions

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.