Results 1 to 7 of 7

Thread: quit on return. [*resolved*]

  1. #1

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    quit on return. [*resolved*]

    hey im trying to make a program that loops though numbers until you hit return, im doing on a console program and im not using cin. Does any one know how this can be done?

    Thanks
    Last edited by john tindell; Dec 27th, 2002 at 05:08 PM.

  2. #2

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    Code:
    #include <windows.h>
    
    ....
    for(;;){
    if(GetAsyncKeyState(VK_RETURN))
    		break;
    	}
    
    ...

  3. #3
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Ack... 100% CPU usage.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Why are you writing a console program and not using i/o functions/objects?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    New Member
    Join Date
    Dec 2002
    Location
    USA
    Posts
    10
    Damn..

  6. #6

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    i just wanted to know how to stop a loop when someone presses a key.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Given that you have a message loop:
    Code:
    while(g_bRun) {
      // ...
    }
    
    // WndProc:
    case WM_KEYDOWN:
      g_bRun = false;
      break;
    Of course this only works if the loop is in a different thread.

    For console apps, this works for me:
    Code:
    while(GetAsyncKeyState(VK_RETURN) == 0) {
    	cout << '.';
    	Sleep(100);
    }
    cout << "\nDone." << endl;
    but it is absolutly not what a console app is intended for.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width