dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: 2 events driving action

  1. #1
    Join Date
    Nov 2011
    Posts
    1

    2 events driving action

    So I have a project I'm working on. There are boxes, all monitored as open or shut (int or bool value). In the project there are three cases. (1) The user opens the correct box (has it's own consequences, but is finished), (2) opens the wrong box and takes out an item, or (3) opens the wrong box and does NOT take out an item. The problem is this: if case 2 occurs, I need to be able to stop the loop. The problem is that I cannot pause the loop to check with something simple like cin, etc. So the idea is that the loop continues unless there is a keystroke, but not necessarily a pausing-type input. I have no idea how to do this, however, or even what I would search for to find something like this.

    IE:

    int boxOpened, rightBox = 3;
    bool itemPicked = false;
    do{


    get_box_status(); // says which box is opened

    /* some kind of check for a space bar stroke (status stored in itemPicked), or an arrow key, or whatever, to indicate they got out an item. if they press the key, or any (whichever method works better) at any point in the loop, the loop ends. */


    } while ((boxOpened != rightBox)&&(!itemPicked));

    I don't know if it's event logging/notification/identification or multithreading (not sure what that is), so I can't even really search for it...

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: 2 events driving action

    You may want to have a look at the console I/O functions, in particular _kbhit(), _getch() and _getche(). To prevent your program from spinning at maximum speed, thereby unnecessarily eating up lots of CPU power, you may consider adding a Sleep() of some milliseconds to your loop.

    Multithreading, of course, is possible in console apps, though uncommon, at least at beginner level. If you don't even know what it is, I woudn't suggest to seriously consider using it for now.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)