[C++] Continuous loop until keypress?

Discussion in 'Programming & Software Development' started by xsive, Oct 27, 2002.

  1. xsive

    xsive Member

    Joined:
    Jun 29, 2001
    Messages:
    4,343
    I'm trying to loop a function continuously until a key is pressed (i'm implementing a manual/automatic run thing).
    getch() is the closest thing I could find to help me achieve this but if i put getch() inside a loop the loop wont work unless theres some kind of input coming in.

    All i end up with is a semi-automatic mode where the program loops while a key is held down. Surely there must be a better way?
     
  2. Geo

    Geo Member

    Joined:
    Jun 27, 2001
    Messages:
    2,368
    Location:
    Adelaide
    Have you tried:

    Code:
    while ( cin.peek() == EOF ) {
             //do stuff
    }
    
    Might work...
     
  3. Jive Honky

    Jive Honky Member

    Joined:
    Jul 30, 2001
    Messages:
    1,390
    Location:
    Melbourne, VIC
    Hmm, maybe have the loop running in a separate thread and use a semaphore? Nah, screw that, too much work, just follow Geo's suggestion. ;)
     
    Last edited: Oct 28, 2002
  4. OP
    OP
    xsive

    xsive Member

    Joined:
    Jun 29, 2001
    Messages:
    4,343
    thanks guys :)
     
    Last edited: Oct 28, 2002
  5. Unregisturd

    Unregisturd Member

    Joined:
    Feb 25, 2002
    Messages:
    1,770
    How would that work though? You'd still need something to check the keyboard and do an up on the semaphore, so you're still going to have something in tight loop...
     
  6. Jive Honky

    Jive Honky Member

    Joined:
    Jul 30, 2001
    Messages:
    1,390
    Location:
    Melbourne, VIC
    Well, I've only done multithreading in C, which is fairly easy, so I'm not sure how to do it in C++. My idea was to have a boolean called 'stop' which is set to false. So in the main thread thread you'd be continually looping until 'stop' is true.
    Code:
    while (!stop)
    {
    // do stuff
    }
    
    In a separate thread, it will just be waiting on input and will set 'stop' to true when it gets it.
    Code:
    cin >> stuff;
    stop = true;
    
    Or something. ;)
     
  7. FlameHead

    FlameHead Member

    Joined:
    Jul 14, 2001
    Messages:
    2,072
    Location:
    Sydney

    That's how I would have done it. But if you wanted to break the loop where it was, id set the loop up in another thread, and when you press they key, an event or just straight input of somesort in the main, it would then teell the loop thread to stop.

    But if that doesn't sound right, just ignore me since I only know Java well.
     
  8. volatile

    volatile Member

    Joined:
    Aug 5, 2001
    Messages:
    32
    Location:
    Chapel Hill, BNE
    I don't know if this works in all C++ compilers, but I've used the _kbhit() function in Visual Studio C++ .NET a fair bit lately.

    Code:
    while (!_kbhit()) {
        //do stuff
    }
    char dummy = getch();
    
     
  9. Unregisturd

    Unregisturd Member

    Joined:
    Feb 25, 2002
    Messages:
    1,770
    Ok, after re-reading the initial post it would seem like threading is a good idea if the program is doing something else. But to check for a keypress, the other thread shouldn't need to loop - it will block waiting for input until it arrives.
     
  10. mertz

    mertz Member

    Joined:
    Oct 28, 2002
    Messages:
    1
    char a;

    while (1) {

    if(cin<<a) { break; }

    }

    just use the return value from the cin object... but my syntax i wrong with the input object im sure.
     
  11. Jive Honky

    Jive Honky Member

    Joined:
    Jul 30, 2001
    Messages:
    1,390
    Location:
    Melbourne, VIC
    Unless I'm mistaken, the code will stop at the if(), waiting for input, instead of continuing, which is not what he wants (unless you have this in a separate thread, in which case why is it in a loop?).

    EDIT: Welcome to the forums mertz. :)

    I'm not quite sure what you mean. The other thread isn't looping; it's just got a simple if() statement, and is stuck waiting for input, once it gets it the thread is finished and the loop in the main thread ends (because the condition is no longer true).
     
    Last edited: Oct 28, 2002
  12. Unregisturd

    Unregisturd Member

    Joined:
    Feb 25, 2002
    Messages:
    1,770
    I was just going off Geo's example... i'm sure there's a better way to do this, but I couldn't be bothered looking up my C/C++ and operating systems books.
     

Share This Page

Advertisement: