This problem really perturbs me on how to do it in C way:
Display series of numbers (1,2,3,4, 5....etc) in an infinite loop. The program should quit if someone hits a specific key (Say Backspace key).
My try was to make a loop and then get the user input every time.
int main()
{
char userInput;
int i = 0;
while(true)
{
Sleep(1000);
printf("%d", ++i);
userInput = getch();
fflush(stdin);
if (userInput == '\b'){
break;
}
}
getch();
return 0;
}
But this is not the answer the problem is looking for. It does want to continue printing numbers while checking the user input.
Anyone could help a newbie with this? Thanks! :D
Update: Mr.Mark Wilkins just gave me the answer by using the _kbhit() function. And this was what my solution looked like:
int main()
{
int i = 0;
char userInput;
while( !_kbhit() && userInput != '\b' )
{
Sleep(500);
printf("%d", ++i);
}
getche();
getch();
return 0;
}