Loop while ENTER is held down exit when released

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Tobias
    New Member
    • Sep 2011
    • 2

    Loop while ENTER is held down exit when released

    I am trying to write a program that goes through a loop while the user holds down the [ENTER] key and EXIT the loop when the key is no longer depressed.
    I got the first part, the loop cycles while the key is depressed but the program just stops when the user no longer holds down the [ENTER] key.
    I’m not even sure it’s possible. Thank you!

    Code:
    double a = 0;
      double b = 3.5;
      double c = 0;
      char ch; ;
      cout << "  PRESS ENTER KEY ";
      ch = cin.get();
      while(ch)
      {
         ch = cin.get();
         c = a * b;
         a = a + 0.01;
         cout << "\n  TOTAL = " << c;
    
    	  if (ch = !cin.get()) // THIS DOESN’T WORK
    	  {
    	     break;
    	  }
      }
      cout << "\n  FINISHED";
      cout << "\n  TOTAL = " << c
    Last edited by Banfa; Sep 16 '11, 12:01 AM. Reason: Added code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You are correct it is not possible, or at least it is not possible using platform independent functions and classes such as cin.

    What exactly do you think this line does ch = cin.get();?
    How about if the user has given no input yet?

    What do you think this line does if (ch = !cin.get())? Break it part method call by method call, operator by operator.

    Comment

    • Tom Tobias
      New Member
      • Sep 2011
      • 2

      #3
      Thank you very much for your confirmation.
      This is part of a somewhat larger program.
      The ch = cin.get(); only gets called when
      the user initiates input.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        True to some extent but cin is often buffered so in fact the program only receives the data from the user when the user presses enter. The the program receives it all at once.

        Comment

        Working...