I know how to use C++ to recieve input via

cin >> x;

But now I want a way to constantly check for input without pressing enter.

To my knowledge with cin you must press enter to assign whatever they typed to a variable.

How could I make it so that when the left arrow key is pressed it adjusts a variable (No need to press enter.)

Thank you! (This is console mode)

As an example of what I want to do...When someone pushes down it would add 1 to row_number. If they push up I want it to subtract from row_number.

Same thing with row_column...

As a note all I want is a tutorial or the code to catch the key...Thanks again!

Recommended Answers

All 2 Replies

>To my knowledge with cin you must press enter to assign whatever they typed to a variable.
Correct. Input in standard C++ is line oriented, so to read raw input you need to use a system-dependent or compiler-dependent solution. This is offers three such solutions, but since you didn't tell us what compiler and operating system you're using, I can't help you any better than that.

>To my knowledge with cin you must press enter to assign whatever they typed to a variable.
Correct. Input in standard C++ is line oriented, so to read raw input you need to use a system-dependent or compiler-dependent solution. This is offers three such solutions, but since you didn't tell us what compiler and operating system you're using, I can't help you any better than that.

Thank You! I am currently using Dev-CPP.

Here is my code

cout << "Please configure the keys!\nTap Left Now";
        int left = getch();
    cout << "Tap Right Now\n";
        int right = getch();
    cout << "Tap Up Now\n";
        int up = getch();
    cout << "Tap Down Now\n";
        int down = getch();

It works nicely except when I push Left it will assign right the same value. How would I stop that? With cin I could do cin.ignore...Is it something similar with this? Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.