-
quit the loop?
How can I stop the loop when user enter ESC? When user enters ESC, the program prompts user for entering Y/N, if Y, stops the loop, otherwise, the loop continue.
Code:
int i = 0;
do
{
i++;
// many sub-programs follow, e.g.
fun1();
fun2();
fun3();
.........
fun10();
}
while(i < 2000); // for example 2000
Because these many sub-programs involves a lot of computations and cannot stop or wait for user's input.
Useless the user press ESC, the loop should be continued to run.
I have no ideas of it.
Can anyone answer me?
-
Here is some info that will help you.
gg
-
it would also work doing this
Code:
#include <stdio.h>
#include <conio.h>
#define ESC 0x1b
int main ( )
{
int i=5;
do{
printf("Press a key.\n");
if(getch()==ESC){
printf("You pressed Escape.\n");
}
else
printf("You didnt press Escape.\n");
i--;
}
while(i>0);
return 0;
}
Pretty much same way;)
-
I've try getch() that the program wait for user's input and cannot jump to
puts("After get ESC"); if no one hit keyboard.
How can I write an program that do not wait for user input?
The program only respond when user hit ESC, otherwise, the program still running.
Code:
int i = 0;
do
{
i++;
puts("Before get ESC");
if(getch()==ESC){
printf("You pressed Escape.\n");
}
else
printf("You didnt press Escape.\n");
puts("After get ESC");
// many sub-programs follow, e.g.
fun1();
fun2();
fun3();
.........
fun10();
}
while(i < 2000); // for example 2000
-
if i understand you then u can do just fine with kbhit...
do{
blabla();
}
while(!kbhit());
-
Yeah, what Fozzy said: use kbhit().
gg
-
You could use _kbhit() to check for a key press first. [edit on] As stated above [edit off]
Code:
#include <stdio.h>
#include <conio.h>
#define ESC 0x1b
int main()
{
int i = 0, stop=0;
while( i < 200000 && !stop )
{
if(_kbhit() && getch()==ESC)
{
stop=1;
continue;
}
printf("PRESS ESC TO STOP %d\n", i);
// many sub-programs follow, e.g.
// fun1();
// fun2();
// fun3();
// .........
// fun10();
i++;
}
return 0;
}