Thread: Replacing a character input with a string and vice versa

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    52

    Replacing a character input with a string and vice versa

    Hello!

    I am making a little game of sorts, and I'd like to include two control options for the character movement.
    The player should be able to either:
    -type "east", "west", "north" or "south" to move in the appropriate direction (I have written the movement function)
    -use the numbers on a keypad instead, thus replacing the directions with 2, 4, 6 and 8.

    Is there any way to make the numbers equal to the strings so that both input methods can be used?

    Also, for bonus points, (though this is pretty much an easier version of the question) is there a way to be able to also substitute "e", "w", "n" and "s" for the directions, while also keeping the full words and the number options?

    Thanks in advance for any help!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Why not just check the input string for each possibility?

    Code:
    if the string is "west", or the string is "w", or the string is "2",
        direction = WEST;
    else if...

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    52
    I hadn't really thought to treat the numbers as strings too xD
    Thanks for that, the simple things are the ones that elude me the most...

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're very welcome. It's not unusual to find applications where you represent "numbers" as strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chaning AM to PM and vice versa
    By EssiJoon in forum C Programming
    Replies: 2
    Last Post: 04-19-2012, 09:36 PM
  2. hash function - int to char string + vice versa
    By kerrymaid in forum C Programming
    Replies: 2
    Last Post: 05-29-2010, 11:01 AM
  3. int to char and vice versa
    By mekaj in forum C++ Programming
    Replies: 13
    Last Post: 12-12-2005, 11:35 AM
  4. Problem: far to near copy, and vice versa
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 06-16-2004, 06:37 AM
  5. Binary to ascii and vice versa
    By thenrkst in forum C++ Programming
    Replies: 13
    Last Post: 03-30-2003, 01:17 AM