Need further guidance

This is a discussion on Need further guidance within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm new to this forum and to C programming as well. I need some guidance from you seniors ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Unhappy Need further guidance

    Hi guys, I'm new to this forum and to C programming as well. I need some guidance from you seniors to help me implement my concept in C programming. What I'm trying to do is using C programming to read which key on the keyboard is pressed and then to check whether the key is (lets just say) "r" or not. If it is "r", the program will proceed with stopping my robot's process. So what I have now is listed below. Please give me further guidance. Thanks in advance

    Code:
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int kbhit(void)
    {
      struct termios oldt, newt;
      int ch;
      int oldf;
    
      tcgetattr(STDIN_FILENO, &oldt);
      newt = oldt;
      newt.c_lflag &= ~(ICANON | ECHO);
      tcsetattr(STDIN_FILENO, TCSANOW, &newt);
      oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
      fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
    
      ch = getchar();
    
      tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
      fcntl(STDIN_FILENO, F_SETFL, oldf);
    
      if(ch != EOF)
      {
        ungetc(ch, stdin);
        return 1;
      }
    
      return 0;
    }
    
    int main(void)
    {
      while(!kbhit())
      printf("You pressed '%c'!\n", getchar());
      return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    So what's stopping you?

    I mean, if you've got that far, it's dead easy to compare the result with say 'r'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    I'm not sure about which command to use :S

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    getchar returns an integer. One of the things that integer can represent is the constant EOF, which signals the end of the file, or input stream. Your code is already reading in a character and checking to see if it was that specific character. The getchar function can also return the ASCII (it's usually ASCII, but keep in mind that it could be whatever encoding your machine happens to use - the C standard doesn't specify an encoding) value of the key that was pressed. You can represent the value for a particular character in C, by something like, 'r'. So if the output of the function matches that value, you know what key they hit.

    edit: I'm guessing you didn't write the code you posted yourself. Make sure you say that in future. It's very confusing and/or inconsistent when someone posts code that does 'x', and they're asking how to do 'x'. If people think you're trying to get out of doing homework, they're less likely to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some guidance.
    By Kinto in forum C Programming
    Replies: 10
    Last Post: 05-31-2009, 12:02 AM
  2. Guidance, please.
    By mattagrimonti in forum C Programming
    Replies: 2
    Last Post: 11-26-2008, 07:50 AM
  3. Need Guidance
    By hYph3n in forum C++ Programming
    Replies: 4
    Last Post: 02-29-2008, 01:09 PM
  4. need guidance to connect to serial port
    By gnychis in forum Linux Programming
    Replies: 1
    Last Post: 06-02-2005, 10:10 AM
  5. Audio guidance.
    By Sebastiani in forum Windows Programming
    Replies: 6
    Last Post: 12-22-2002, 08:14 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21