>What i want is something which wont wait for the user after a fixed time..
Most likely you were trying to do it manually, so something like this should give you ideas:
#include <ctime>
#include <iostream>
#include <string>
#include <conio.h>
template <typename T, typename Func>
T timed_input ( Func reader, int seconds )
{
clock_t start = clock();
while ( clock() < start + ( seconds * CLOCKS_PER_SEC ) ) {
if ( kbhit() )
return reader();
}
return T();
}
struct line_reader {
std::string operator()()
{
std::string s;
if ( getline ( std::cin, s ) )
return s;
return std::string();
}
};
int main()
{
std::cout<<"Enter your name in less than five seconds: ";
std::string name = timed_input<std::string> ( line_reader(), 5 );
if ( name.empty() )
std::cout<<"\nTime's up\n";
else
std::cout<<"You entered \""<< name <<"\"\n";
}
This is actually more user friendly than straight raw input, because once a key is pressed, the program will start blocking for input. This gives the user as much time as necessary after the first keypress to finish typing. If you don't want that, a combination of kbhit and getch can be used to time the entire process, but you also have to do more work in error handling.
>My point was that with Visual C++, you can now leave off the .h and put this
Actually, you have no choice. Current versions of Visual C++ won't accept old-style C++ headers.