Click to See Complete Forum and Search --> : small doubt about cin>>


seshreddy
April 30th, 2003, 12:01 AM
Hi ,

We know that when we want to get input from console most frequently used one is cin and it waits infinitely for input.

But I want to use cin to get input without waiting more than specified time.That time may be some thing like after 5 sec.

Is it possible or not?

If so, can any one tell me the way to do it?


with regards...
seshu

muthuis
April 30th, 2003, 03:51 AM
Nope! there is no time specific input readers.

seshreddy
April 30th, 2003, 04:38 AM
how it will be if u use operator overloading?
but what happens when we want to infinitely for the input in some conditions of the application.

please think abnout it..


with regards...
seshu

muthuis
April 30th, 2003, 07:37 PM
Sesh,
I am not really sure how overloading will be effective in this matter. A possible workaround could be running a separate thread with our regular cin to wait for the input. The parent process can do a Waitforsingleobject(handle,SPECIFIEDTIME);
If the input has been entered, return and come out. Otherwise kill the thread.
I am not really sure abt the overloading idea u r trying to say.

mwilliamson
April 30th, 2003, 08:40 PM
Would a cout be read by cin? Maybe you test this:

#include <iostream>
#include <string>

using namespace std;

void cin_timeout( void* pVoid )
{
long timeout = pVoid;
Sleep( timeout );
cout << "timeout";
}

int main()
{
string s;
_beginthread( cin_timeout, 0, 5 );
cin >> s;
return 0;
}

seshreddy
May 1st, 2003, 08:37 AM
Hi,

thank u all.

i tried to run the program using thread but i am getting error C2065: '_beginthread' : undeclared identifier

this is vc++ compiler and windows 2000.

this is the code that our friend has given abd which i too tried.

#include <iostream>
#include <string>
#include <windows.h>
#include <process.h>

using namespace std;

void cin_timeout( void* pVoid )
{
long timeout = (long)pVoid;
Sleep( timeout );
cout << "timeout";
}

int main()
{
string s;

_beginthread( cin_timeout, 0, 5 );
cin >> s;
return 0;
}

bye
seshu

mwilliamson
May 1st, 2003, 11:52 AM
You need to compile with /MT, but I just tried and it doesn't work :(

mahanare
May 2nd, 2003, 01:27 AM
you may get rid of the error with proper settings. i feel.

If you try your code in a MFC supported console application will do the work.
start a console project but in the next step select "an application that supports MFC" this will start a console application with tmain()

then you can do the work.
this is may work as a solution for your error.
and don't forget to add #include<process.h>
it solves the problem of the compiler error

cheers
mahanare

seshreddy
May 2nd, 2003, 05:31 AM
hello



how can we do it with mfc support console application?

i tried to do but again same error is coming...

look at this..

bye
seshu