I'm having a problem with a cin function.
My program is the game "snake", all done using an ascii display.
It uses WASD to move and Q to quit.
The problem I'm having is that after a single game is done (when you die), it asks for your name to put it into the highscore table (saved on a .txt file).
When it asks for a name input, after saying "Please enter your name: ", where it should let you type your name, it types every key you pressed while playing the game, and THEN lets you type your name. When it does this, the unintended displayed characters are deletable, you simply backspace to get rid of them.
My guess is that whenever I press a key, it saves the key into the cin storage or something like that, and assumes that they're part of the input that I want, but I tried using the cin.sync() and cin.clear() commands and they didn't change the outcome.
Any help is appreceated, and any questions will be answered to the best of my ability.
How I get the input key:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
//gets the pressed key
if (GetAsyncKeyState(0x57)){ //checks W
if(key!="down"){
key = "up";
}
}
else if (GetAsyncKeyState(0x44)){ //checks D
if(key!="left"){
key = "right";
}
}
else if (GetAsyncKeyState(0x53)){ //checks S
if(key!="up"){
key = "down";
}
}
else if (GetAsyncKeyState(0x41)){ //checks A
if(key!="right"){
key = "left";
}
}
else if (GetAsyncKeyState(0x51)){ //checks Q
key = "quit";
exit;
rockx[9001]=0; //TROLLEXIT. calls a variable outside the array
}
else if (GetAsyncKeyState(VK_SPACE)){
keybackup = key;
paws = true;
}
else if (GetAsyncKeyState(0x50)){ //checks P
//score++;
foodlive = false;
//creates a new rock every 5 points
rockcount++;
if(rockcount==5){
rockcount=0;
rocklive = false;
}
}
//moves the guy
if (key == "up"){
if (ycoor > 2){
ycoor = ycoor-1;
}
else if (ycoor == 1){
key = "quit";
}
}
else if (key == "down"){
if (ycoor < ybound-1){
ycoor = ycoor+1;
}
else if (ycoor == ybound-1){
key = "quit";
}
}
else if (key == "left"){
if (xcoor > 1){
xcoor = xcoor-1;
}
else if (xcoor == 1){
key = "quit";
}
}
else if (key == "right"){
if (xcoor < xbound-1){
xcoor = xcoor+1;
}
else if (xcoor == xbound-1){
key = "quit";
}
}
|
The name entering is a simple cin statement, but I thought I should include it just to be thorough:
1 2 3
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout<<"Enter your name: ";
cin>>playername;
|