Thread: Need help with input char

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    37

    Unhappy Need help with input char

    hello, my code (as follows) is not working. I am trying to give a different message for each of the different wsad keys that are pressed (to turn into a game later on). The current code is as follows:

    Code:
     char up_key=="w";
    char down_key=="s";
    char left_key=="a";
    char right_key=="d";
    int c;
    
    void main()
    
    {
    
    c==getch();
    
    if (c==up_key);
    goto up;
    
    dead:  
     {
     printf ("ZZZZZZZZZZZ");
      system("pause");
     }
      
      up:
      {
      printf ("ello");
      printf("\nThe letter number for %c is %d. Press a key.");
      system("pause");
      }
      }
    I have heard of giving each key a different # value, but would like to take the easy rout if possible by having the key presses recognized. Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, since you didn't tell us what's wrong, here's a list of problems I can find just by looking:
    1. You need to #include <stdio.h> for getch() and printf(), and you need to #include <stdlib.h> for system().
    2. It's int main(void) and return an int at the end, usually 0.
    3. Global variables are evil.
    4. Goto should be avoided at all costs.
    5. A double == is for comparison, a single = for assignment. You only need one equal sign in c = getch().
    6. printf needs an extra parameter for each format specifier (% thing), like so: printf("\nThe letter... Press a key.", c, c).
    7. Learn about loops. There's a tutorial here, and Google has tons more.

    That's all I got in my 30-second evaluation. There may be more.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    That's all I got in my 30-second evaluation. There may be more.
    Yep... quotes instead of apostrophes for characters.

    And I hope you won't mind if I repeat something you said... goto is surrender, not programming!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Tater: Ahh, good catch...I missed the double == up there as well (which should be a single =, drewtoby). As soon as I recognized they were globals, I discarded the line as "broken, needs fixin".

    @drewtoby: I get the impression you are just jumping in the deep end, hoping you'll somehow magically know how to swim. There are some pretty egregious errors in there that suggest you haven't read a halfway decent book or tutorial yet. We have some basics tutorials for you here, and Google will find many more. I strongly suggest you sit down and go through all the sections, in order, and work through the examples until you actually know this stuff. You'll appreciate it later on when you're trying to write your game.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    37
    Okay, thanks!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting const char* as an input
    By milanbarosh in forum C Programming
    Replies: 9
    Last Post: 04-24-2008, 01:33 AM
  2. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  3. Function input may char or char[] ..?
    By GSalah in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2006, 04:07 AM
  4. prevent char input from int
    By SuperNewbie in forum C Programming
    Replies: 1
    Last Post: 04-08-2004, 02:35 AM
  5. Char input
    By mkeef in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 03:54 PM