-
scan codes and getch
it always displays going up no matter what key is pressed can anyone tell me why?
edit: the keys i want to be used are w a s d
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
/* variables */
int key;
/* variables end */
top:
if (kbhit())
{
key = _getch();
if (key = 119)
{
cout<<"Going Up \n";
}
else if (key = 115)
{
cout<<"Going down \n";
}
else if (key = 97)
{
cout<<"Going left \n";
}
else if (key = 100)
{
cout<<"Going right \n";
}
}
goto top;
return 0;
}
-
Code:
if (kbhit())
{
key = _getch();
if (key = 119)
{
cout<<"Going Up \n";
}
else if (key = 115)
{
cout<<"Going down \n";
}
else if (key = 97)
{
cout<<"Going left \n";
}
else if (key = 100)
{
cout<<"Going right \n";
}
}
Use == for comparison. You're assigning 119 to key every time.
-
and, on a lighter note, comments like "/*start variables */" are relatively worthless, because they add nothing to the users comprehension to the code. Comments should supplement the code.
and also, who else has noticed the rash of problems EXACTLY like this one?
-
-
i just keep /* start variables */ for my use no one is learning anything from my code
-
> no one is learning anything from my code
You never know.
If your scanning for input, then looping each time, you could try a switch/case statement.
However, it's off the top of my head and I don't know how well it will work.
As far as checking for scan codes, I've found this chart to be decent.