Help please.

I've heard it is not possible in C++, but I'd like to be able to have a simple command where the user presses a key (preferably a number) and code executes. Is there a simple command? I'm using windows and iostream as includes, so if the command takes something else please tell.

I am using Windows 2000, and I have access to XP and Vista. I'm using Dev Bloodshed C++.

If it is possible, I would like to know two commands. One that stops and waits for multiple keypresses, such and 1 or 2, and one that you press to stop a loop and doesn't stop the thread to wait for it.

I know it's picky, but either would help. Anything with keypresses would help, actually.

Comments

  • : I've heard it is not possible in C++, but I'd like to be able to
    : have a simple command where the user presses a key (preferably a
    : number) and code executes. Is there a simple command? I'm using
    : windows and iostream as includes, so if the command takes something
    : else please tell.
    :
    : I am using Windows 2000, and I have access to XP and Vista. I'm
    : using Dev Bloodshed C++.
    :
    : If it is possible, I would like to know two commands. One that
    : stops and waits for multiple keypresses, such and 1 or 2, and one
    : that you press to stop a loop and doesn't stop the thread to wait
    : for it.
    :
    : I know it's picky, but either would help. Anything with keypresses
    : would help, actually.
    :

    Try this (Please note, it is not ANSI compatible):
    [code]
    #include

    //...

    while (1) {

    if (getch()=='1') // is the key down? If so, break
    break;
    }
    [/code]

    Im not sure what you mean by "Multiple keypresses", though.
    twos keys down at the same time?

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website :: OS Development Series[rightbr][/link][/size]
  • I will try that now, thank you.

    No, I mean something such as a menu. If you press one, do this action.
    If you press two, do this action.

    Edit: I've gotten it so that I can press 1 and it will execute the specified code, and if I press 2 twice in a row in does the specified code.

    A way you can think of so I don't have to press it twice?
    Here is my current testing code.
    [code]#include
    #include

    int main() {
    back:
    if (getch()=='1') {
    cout<<"You pressed 1.";
    cin.get();
    system("cls");
    goto back;
    }
    if (getch()=='2') {
    cout<<"You pressed 2.";
    cin.get();
    system("cls");
    goto back;
    }
    }
    [/code]
    I have used this same pattern for up to 3 (1, 2, and 3 being seperate) bits of code. The third one I have to press 3 times. Am I just stuck with this?
    =================================
    Noob programmer at Actionscript and C++.

    ~Always needing help~
  • I recommend looking at creating a state manager to manage menu
    states. Heres the basic theory:
    [code]
    enum {
    STATE_NONE= 0x0,
    STATE_1= 0x0001,
    STATE_2= 0x0002
    };

    static int iState=STATE_NONE;
    while (1) {

    // get input
    if (getch()=='1')
    iState=STATE_1;

    if (getch()=='2')
    iState=STATE_2;

    // check current state
    switch (iState) {

    case STATE_NONE:
    break;

    case STATE_1:

    cout << "You pressed one!";
    cin.get();
    system ("cls");
    break;

    case STATE_2:

    cout << "You pressed two!";
    cin.get();
    system ("cls");
    break;
    }
    }
    [/code]
    This is more readable, and does not have ugly gotos.
    If you use the STL, you can easily store states into an
    extendable manager via std::vector<>.

    For a side note, I recommend using [b]clrscr()[/b] instead of
    [b]system()[/b] from conio.h, as clrscr() is more portable:
    [code]
    clrscr (); // clear screen
    [/code]

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website :: OS Development Series[rightbr][/link][/size]
  • The code still didn't work the way I wanted it to.

    Now I have to press anything and then 1 or 2.

    What is the header file you need to use clrscr(); ?

    =======================================
    Noob programmer at Actionscript and C++.

    . ~Always needing help~ .
  • Oops, double post.

    =======================================
    Noob programmer at Actionscript and C++.

    . ~Always needing help~ .
  • What is your current code? Please use [leftbr]code[rightbr]*Your code here*[leftbr]/code[rightbr] when posting :-)

    Also, [b]clrscr()[/b] is inside of [b]conio.h[/b].

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website :: OS Development Series[rightbr][/link][/size]
  • : What is your current code? Please use [leftbr]code[rightbr]*Your
    : code here*[leftbr]/code[rightbr] when posting :-)
    :
    : Also, [b]clrscr()[/b] is inside of [b]conio.h[/b].
    :
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website
    : :: OS Development Series[rightbr][/link][/size]
    :
    I mean the code you gave me. It didn't work. I have to press any key then 1 or 2. Is it my compiler? I use dev bloodshed C++.
    =======================================
    Noob programmer at Actionscript and C++.

    . ~Always needing help~ .
  • : I mean the code you gave me. It didn't work. I have to press any
    : key then 1 or 2. Is it my compiler? I use dev bloodshed C++.

    What is *your* current code? The code I gave you was not complete.

    Also, I don't think it is the compilier at all.

    Im posting this from work. Once I get back, Ill post workable code for
    you to try.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website :: OS Development Series[rightbr][/link][/size]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion