Thread: scan codes and getch

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    34

    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;
    }
    Compiler: MSVC++ 6.0

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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.

  3. #3
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    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?

  4. #4

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    34
    i just keep /* start variables */ for my use no one is learning anything from my code
    Compiler: MSVC++ 6.0

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > 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.
    Last edited by Shadow; 09-13-2002 at 09:41 PM.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-12-2008, 12:40 PM
  2. using double codes in assigning value to an int variable
    By hitesh_best in forum C Programming
    Replies: 10
    Last Post: 08-02-2007, 01:12 AM
  3. Question about ASCII codes
    By rwmarsh in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2006, 10:49 AM
  4. Getting ASCII codes
    By moonwalker in forum C Programming
    Replies: 3
    Last Post: 07-24-2002, 01:17 PM
  5. ...keyboard codes...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-05-2002, 04:23 PM