Thread: how to pass values into getchar in gdb

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    how to pass values into getchar in gdb

    I'm working through poker.c from King's C programming book and I need help analyzing it via gdb

    He has the following:
    Code:
     while (cards_read < NUM_CARDS) {
         bad_card = false;
    
         printf("Enter a card: ");
    
         rank_ch = getchar();
    When gdb gets to rank_ch = getchar(), how do I pass a value (9s for 9 of spades for example) into gdb?

    full source code: http://knking.com/books/c2/programs/poker.c

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    Have you tried pressing 9 and then enter?
    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.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    58
    Hallo bos1234!

    I hope this code can help you, ignore the german comments.
    Code:
    /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
    /*
     * main.cc
     * 
     * chkkeystroke is free software: you can redistribute it and/or modify it
     * under the terms of the GNU General Public License as published by the
     * Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     * 
     * chkkeystroke is distributed in the hope that it will be useful, but
     * WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     * See the GNU General Public License for more details.
     * 
     * You should have received a copy of the GNU General Public License along
     * with this program.  If not, see <http://www.gnu.org/licenses/>.
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/select.h>
    #include <termios.h>
    
    #include <fcntl.h>
    
    struct termios orig_termios;
    
    void reset_terminal_mode()
    {
        tcsetattr(0, TCSANOW, &orig_termios);
    }
    
    void set_conio_terminal_mode()
    {
        struct termios new_termios;
    
        /* take two copies - one for now, one for later */
        tcgetattr(0, &orig_termios);
        memcpy(&new_termios, &orig_termios, sizeof(new_termios));
    
        /* register cleanup handler, and set the new terminal mode */
        atexit(reset_terminal_mode);
        cfmakeraw(&new_termios);
        tcsetattr(0, TCSANOW, &new_termios);
    }
    
    int kbhit()
    {
        struct timeval tv = { 0L, 0L };
        fd_set fds;
        FD_ZERO(&fds);
        FD_SET(0, &fds);
        return select(1, &fds, NULL, NULL, &tv);
    }
    
    // normaly 1, but better ch, so we have the ASCII-value;
    int xkbhit(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 ch;// Normaly return value  1, here the ASCII-value 
      }
     
      return 0;
    }
    
    /// reads from keypress, doesn't echo
    int getch(){
        struct termios oldt, newt;
        int ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
    }
    
    int own_getstring(char wort[])
    {
     int rewer = 0, taste = 0, i = 0; 
    
      while ((taste == 10) || (taste == 13))
        {
         taste = xkbhit(); 
    
         if ((taste == 10) || (taste == 13))
          {        
           rewer = 1;
           } 
           else  
            {
             wort[i++] = (char)taste;
             wort[i] = '\0';   
            }    
        }
    
     return rewer;    
    }
    
    int main(int argc, char *argv[])
    {
      int taste = 0, i = 0;
        char wort[200] = {0};
        //set_conio_terminal_mode(); // Wenn nicht gesetzt dann bei gedrueckter Enter-Taste 
    /*                               // with xkbhit returnvalue 10, if set then 13
    while (!xkbhit())
        {}
           printf("%c", taste);        
        */
    
       for (i = 0; i < 20; i++)    
        {
         while (taste == 0)
          taste = xkbhit(); 
          usleep(100000); //<--------------------you can modify these number, try and see  
         printf("%c", taste);  
         taste = 0; 
       }     
        
    
        printf("***%c = %d    i=%d\n", taste, taste, i);
       //taste = own_getstring(wort);
       //printf("Enter was: %s\n", wort);
    
    getch();
        printf("Program end\n");
    
        
        return EXIT_SUCCESS;
        
    }
    It was written for SUSE Linux tumbleweed, but i hope, it can help you.
    Last edited by rusyoldguy; 08-03-2020 at 08:05 AM.

  4. #4
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    58
    other example:
    Code:
    /* getchar example : typewriter */
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
      int c;
      puts ("Enter text. Include a dot ('.') in a sentence to exit:");
      do {
        c=getchar();
        putchar (c);
      } while (c != '.');
      return 0;
    }
    here the second better version of the source above:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/select.h>
    #include <termios.h>
     
    #include <fcntl.h>
     
    struct termios orig_termios;
     
    void reset_terminal_mode()
    {
        tcsetattr(0, TCSANOW, &orig_termios);
    }
     
    void set_conio_terminal_mode()
    {
        struct termios new_termios;
     
        /* take two copies - one for now, one for later */
        tcgetattr(0, &orig_termios);
        memcpy(&new_termios, &orig_termios, sizeof(new_termios));
     
        /* register cleanup handler, and set the new terminal mode */
        atexit(reset_terminal_mode);
        cfmakeraw(&new_termios);
        tcsetattr(0, TCSANOW, &new_termios);
    }
     
    int kbhit()
    {
        struct timeval tv = { 0L, 0L };
        fd_set fds;
        FD_ZERO(&fds);
        FD_SET(0, &fds);
        return select(1, &fds, NULL, NULL, &tv);
    }
     
    // normaly return value is 1, but better ch, so we have the ASCII-value;
    int xkbhit(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 ch; // Normaly return value  1, here the ASCII-value 
      }
      
      return 0;
    }
     
    /// reads from keypress, doesn't echo
    int getch(){
        struct termios oldt, newt;
        int ch;
        tcgetattr( STDIN_FILENO, &oldt );
        newt = oldt;
        newt.c_lflag &= ~( ICANON | ECHO );
        tcsetattr( STDIN_FILENO, TCSANOW, &newt );
        ch = getchar();
        tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
        return ch;
    }
     
    int own_getstring(char wort[])
    {
     int rewer = 0, taste = 0, i = 0; 
     
      for (i = 0; i < 100; i++)
      { 
       while (taste == 0) taste = xkbhit(); 
       usleep(180000); //<--------------------you can modify these number, try and see    
       taste = getch();
       
       if ((taste == 10) || (taste == 13))
        return i;
         
       printf("%c", taste);  
       wort[i] = (char)taste;
       wort[i + 1] = '\0';   
      }
     
     return rewer;    
    }
     
    int main(int argc, char *argv[])
    {
      int taste = 0, i = 0;
        char wort[200] = {0};
        //set_conio_terminal_mode(); // if not set then returnvalue by Enter-button with xkbhit returnvalue 10, if set then 13
     
       for (i = 0; i < 20; i++)    
        {
         while (taste == 0) taste = xkbhit(); 
          taste = getch(); 
          usleep(180000); //<--------------------you can modify these number, try and see  
          if (taste == 10) break;
         printf("%c", taste);  
         wort[i] = taste;
         taste = 0; 
       }     
         
     
       printf("\nfirst Enter was: %s\n", wort);
       taste = own_getstring(wort);
       printf("\nsecond Enter was: %s\ni=%dn", wort, taste);
     
       getch();
       
       printf("Program end\n");
     
       return EXIT_SUCCESS;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass values to a dateTimePicker control
    By silkyt in forum Windows Programming
    Replies: 1
    Last Post: 11-04-2007, 07:15 PM
  2. How to pass enum values
    By Bargi in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2007, 02:55 AM
  3. Pass values to C++ dll and return result
    By joeyzt in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2004, 11:26 PM
  4. howto pass values to & from
    By merlyn2000 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 06:31 PM

Tags for this Thread