cin w/o enter

Im making an ASCII game, and I'm not very good, and I would like to know what I can use to take input without having to press enter.

Here's one of my functions (The rest are exactly the same, but with different outputs and cases):
1
2
3
4
5
6
7
8
9
10
void v11713(){
ClearScreen();
cout << "PPPPP___________Q_______________\nPPPPPP_____________/ \__________\nPPPP_______________|b|__________\nPPPPP_______~~~~~_______________\nPPP________~~~~~~~______________\nPPP_______~~~~~~~~~_____________\nP__________~~~~~~~______/ \_____\nP___________~~~~~_______|a|_____\n________________________________\n____/ \_________________________\n____|c|_________________________\n________________________________\n________________________________";
cin >> input;
switch (input){
case 4:
v11613();
break;
};
}


Iknow it's not very good, but would like help alot, so Thanks in advance!
Try getch(); in <conio.h>.

Hope it helps,
Aceix.
I can't figure that out... How would I use it?
An example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

int main()
{
     cout<<"Press any key: ";
     getch();  //waits for the user to hit a key
     if(GetAsyncKeyState(VK_DOWN))  //if the user hit the down arrow key
           cout<<"\nYou pressed the down arrow key";

     return 0;
}


Hope it helps,
Aceix.
But how would I use it in that block of code that I posted?

Here it is again:

Here's one of my functions (The rest are exactly the same, but with different outputs and cases):


1
2
3
4
5
6
7
8
9
10
void v11713(){
ClearScreen();
cout << "PPPPP___________Q_______________\nPPPPPP_____________/ \__________\nPPPP_______________|b|__________\nPPPPP_______~~~~~_______________\nPPP________~~~~~~~______________\nPPP_______~~~~~~~~~_____________\nP__________~~~~~~~______/ \_____\nP___________~~~~~_______|a|_____\n________________________________\n____/ \_________________________\n____|c|_________________________\n________________________________\n________________________________";
cin >> input;
switch (input){
case 4:
v11613();
break;
};
} 
Last edited on
Looks like we must change our mind about the getch();, and look for a different function.

When I find one I will inform you.

Thanks,
Aceix.
1
2
3
4
5
6
7
8
9
    char input = getch();
    switch (input){
        case '4':
            cout << "Four\n";
            break;
        default:
            cout << "Default\n";
            break;
    }
if I do it like that, after the menu, when i bring up the game, the avatar won't move, when it did before.
Ah, I see.
Then try this instead:
1
2
3
4
    char ch;
    if (kbhit()) {
        ch = getch();
    }

The only real problem is that these functions are part of <conio.h> which is non-standard and may vary or not exist at all depending on the compiler.

Function kbhit() (if it's available) checks whether a key has been hit, If not, it doesn't wait, but instead allows the program to continue.
Last edited on
It freaked out and started flashing.
Probably means it was running successfully, but very fast.

Without seeing more of your code, it's hard to say what's happening. But if you have a main loop which goes around and around, updating the screen and checking for keystrokes, chances are it's going faster than you actually need.

I'm totally guessing here. You might try putting in a Sleep(n); delay in there where n is a number of milliseconds.
You are using Windows, right?

Use iskeypressed() to see if there is any input waiting to be read:
http://www.cplusplus.com/forum/beginner/5619/#msg25047

Actually getting the key is a little more involved. I don't have much time this moment to give you something exactly what you need, but you have to play with "unbuffered input" to do it. Here are a few threads to help get started:

http://www.cplusplus.com/forum/beginner/3329/
http://www.cplusplus.com/forum/beginner/3329/

Good luck!
Topic archived. No new replies allowed.