title: Stop a loop by pressing a key in real-time
body: <p>I have written a simple timer. By pressing "s" (without pressing enter for submitting it) the timer (for-loop) will start. It is a no-ending loop. I want to stop it as soon as user presses "s", just like in the way I start it. As soon as pressing "s" (without pressing enter for submitting it) the loop should stop. How to do it?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;conio.h&gt;
  using namespace std;
  
  int main()
  {
      char ch;
      int m=0,h=0;
      if ((ch = _getch()) == 's')
          for (int i=0;;i++)
          {
              cout &lt;&lt; "h   m   s" &lt;&lt; endl;
              cout &lt;&lt; h &lt;&lt; "   " &lt;&lt; m &lt;&lt; "   " &lt;&lt; i;
              system("CLS");
              if (i==60) {i=0;m+=1;}
              if (m==60) {m=0;h+=1;}
              if (h==24) {h=0;}
          }
      return 0;
  }
  </code></pre>