Click here to Skip to main content
15,566,240 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hey Guys, How do I make this WASD code C, and also simplified? Im not smart im sorry.

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    char ch=0;
    cout << "Press Q to quit\n";
    do
    {
        ch = getch();

        switch(ch)
        {
            case 'W':
            case 'w':
                cout << "W was pressed \n";
                break;
            case 'A':
            case 'a':
                cout << "A was pressed \n";
                break;
            case 's':
            case 'S':
                cout << "S was pressed \n";
                break;
            case 'D':
            case 'd':
                cout << "D was pressed \n";
                break;

        }

    }while (ch != 'Q' && ch!='q');
}


What I have tried:

I have done research, but I think I need a human to human answer. Cout does not work in C
Posted
Updated 6-Mar-21 7:48am
Comments
Rick York 5-Mar-21 11:15am    
Still haven't figured out tolower eh? All right then.
Richard MacCutchan 5-Mar-21 11:22am    
Why are you mixing conio with iostream? Use the very basic C libraries or the proper C++ libraries but don't mix the two.

C uses printf(), which is found in the <stdio.h> header. Since you appear to be using Windows, you may need to change getch() to _getch()
 
Share this answer
 
Comments
[no name] 5-Mar-21 11:25am    
Thanks!
Here is code you can use to change a character to lower case so you don't have to check for both cases all the time.
C++
inline int ToLower( int ch )
{
    const int diff = 'a' - 'A';
    if( ( ch >= 'A' ) && ( ch <= 'Z' ) )
        ch += diff;
    return ch;
}
This function exists in the standard C run-time library but I wanted you to see what it does so you can be encouraged to use it in future. I changed the name so it can co-exist in case you want to use this. One can make a similar function for toupper.
C++
inline int ToUpper( int ch )
{
    const int diff = 'a' - 'A';
    if( ( ch >= 'a' ) && ( ch <= 'z' ) )
        ch -= diff;
    return ch;
}
They rely on the fact that the ASCII code for the characters of the two cases are parallel and a certain number of places apart - the diff value.

With that, you could use a function like this :
C++
// in WinUser.h you will find these definitions : 
// #define VK_LEFT           0x25
// #define VK_UP             0x26
// #define VK_RIGHT          0x27
// #define VK_DOWN           0x28
// you can use them or define your own.
// VK stands for virtual key

int GetWASDvalue( int input )
{
    int chlow = ToLower( input );
    switch( chlow )
    {
        case 'a' : return VK_LEFT;
        case 'w' : return VK_UP;
        case 'd' : return VK_RIGHT;
        case 's' : return VK_DOWN;
        // there are also VK_HOME and other values that can be added
    }
    return 0;   // not recognized
}
You can feed this function an input character and it will give you a direction.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900