title: How do you write a C program to increment a number by keypress and auto-decrement it per second?
body: <p>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.</p>
  
  <p>Problem with my approach is that nothing happens until I press a key (that is, it checks if anything is pressed with <code>getch()</code>). How do I check that nothing is pressed? And of course, <code>!getch()</code> doesn't work because for that to work, it'll still need to check for keypress which nullifies the purpose itself.</p>
  
  <p>OS: Windows 10 Enterprise, 
  IDE: Code::Blocks</p>
  
  <pre><code>void main()
  {
      int i, counter = 0;
      for (i = 0; i &lt; 1000; i++)
      {
          delay(1000);
          // if a key is pressed, increment it
          if (getch())
          {
              counter += 1;
              printf("\n%d", counter);
          }
          while (counter &gt;= 1)
          {
              if (getch())
              {
                  break;
              }
              else
              {
                  delay(1000);
                  counter--;
                  printf("\n%d", counter);
              }
          }
      }
  }
  </code></pre>