I'm trying to write a program in which a number starts from 0, but when you press any key, it gets incremented by 1. If nothing is pressed, it keeps on decreasing by 1 per second until it reaches 0. Every increment or decrement is displayed on the console window.
Problem with my approach is that nothing happens until I press a key (that is, it checks if anything is pressed with getch()
). How do I check that nothing is pressed? And of course, !getch()
doesn't work because for that to work, it'll still need to check for keypress which nullifies the purpose itself.
OS: Windows 10 Enterprise, IDE: Code::Blocks
void main()
{
int i, counter = 0;
for (i = 0; i < 1000; i++)
{
delay(1000);
// if a key is pressed, increment it
if (getch())
{
counter += 1;
printf("\n%d", counter);
}
while (counter >= 1)
{
if (getch())
{
break;
}
else
{
delay(1000);
counter--;
printf("\n%d", counter);
}
}
}
}
delay()
? – Ninepins