sorry about my english
i make a game and i need to use on interrupt i/o(input), someone can to help me??

the code is :

cout<<"Enter num : ";


for(i=1; i<=100000000; i++){//its loop time 


// here the interrupt // when the user click some key its 
//stop the loop and goto input(num)
//but when the time over its stop and say him "you are loose" and exit

MaxTime--;
if(MaxTime == 0)
{
cout<<"You are losse"<<endl;
exit(1);
}
}

cin>>num;

First of all, this: for(i=1; i<=100000000; i++){//its loop time is a horrible way to create a 'timer'. You could better use something like:

#include <ctime>
//[...other code..]

time_t t1= time(0), t2= time(0);
while( (t2 - t1) < 5) 
    t2 = time(0);
// here 5 seconds have passed

or int other words:

#include <ctime>
//rest of the code

std::clock_t start;
start = std::clock();
while (std::clock() - start < 5000)
    ;
//here 5 seconds have passed

Or one of the other options you can find on google.

Second: you're using a whole bunch of undeclared variables.

For your original question, you might want to have a look at getch() . It's a non-standard function that takes in input and doesn't wait for a enter to be pressed

>First of all, this: for(i=1; i<=100000000; i++){//its loop time >is a horrible way to create a 'timer'.
Yes indeed. It relies too heavily on the clock speed of the machine. For example, I have busy loops constructed like this that ran for exactly one second in the mid 1990s, but now it runs much faster. An upgrade in the computer can break your program without changing the code, which isn't a good way to live.

>You could better use something like
That essentially solves the inaccuracy problem (though your examples are technically non-portable due to assumptions about time_t and clock_t), but the antisocial problem of a busy loop is still present. On a multitasking system (which most are these days), you should be sleeping the thread so that other tasks can run rather than working the CPU so that your task takes full control.

That essentially solves the inaccuracy problem (though your examples are technically non-portable due to assumptions about time_t and clock_t), but the antisocial problem of a busy loop is still present. On a multitasking system (which most are these days), you should be sleeping the thread so that other tasks can run rather than working the CPU so that your task takes full control.

You are right (as always), but I thought I'd show one of the more 'easy' ways to use a timer. A multi-threaded solution would be out of the OP's league in my opinion.

@OP: If you want a (more) portable solution, you might want to take a look at the boost timer from the boost-library . Although, I've not yet worked with it, I have heard some good things about it

ctime its not good for me . i need when the loop run (this time) and the user of the game enter anny key the loop stop (and the time stop) and this go to input....
but when the time over (the loop over) the user of the game losse...

i am need for this interrupt ...
thank..

I think the OP is trying to read the keyboard without blocking on a key being pressed.

It's possible, but it depends on your OS and compiler as to how you do it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.