Thread: How to debug a program with char input

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    How to debug a program with char input

    I am trying to debug the following program with GDB. The program takes character inputs as you can see, but once I am in GDB shell, the inputs are interpreted by GDB. How do I address this please?

    Here is the program:

    Code:
    #include <stdio.h>
    int main ()
    {
    char ch;
    printf ("\nPress any key to continue");
    getch();
    printf ("\nType any character");
    ch = getche();
    printf("\nType any character");
    getchar();
    printf("\nContinue Y/N");
    fgetchar();
    }

    The problem I have is fgetchar() does not seem to do anything or at least what I want it to do, which is to echo the character that I type in. After I put in 3 characters, the program stops. I realize these are old dos functions, and I am using MinGw on Windows 10. So the program executes. I tried to run gdb, but I am not sure what I can do to see how the function call behaves.

    Here you'll see that I had some problem with knowing exactly where gdb was in program execution, so I typed in some strange commands but I learned my way around it:

    Code:
    [New Thread 8112.0x18dc]
    [New Thread 8112.0x19d0]
    Breakpoint 1, main () at second.c:5
    5       printf ("\nPress any key to continue");
    (gdb) n
    Press any key to continue6      getch();
    (gdb) f
    #0  main () at second.c:6
    6       getch();
    (gdb) n
    8       printf ("\nType any character");
    (gdb) d
    Delete all breakpoints? (y or n) n
    (gdb) n
    Type any character9     ch = getche();
    (gdb) w
    Ambiguous command "w": watch, whatis, where, while, while-stepping, ws.
    (gdb) n
    w11     printf("\nType any character");
    (gdb) n
    Type any character12    getchar();
    (gdb) n
    r
    13      printf("\nContinue Y/N");
    (gdb) n
    Continue Y/N14  fgetchar();
    (gdb) n
    15      }
    (gdb)
    0x00401250 in __mingw_CRTStartup ()
    (gdb) n
    Single stepping until exit from function __mingw_CRTStartup,
    which has no line number information.
    [Inferior 1 (process 8112) exited with code 012]
    (gdb) n
    The program is not being run.
    (gdb) quit

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    First thing first. Your code donst even compile in the first place. The function your calling getch, getche etc are defined in conio.h which is a non standard library and therefore the compiler would start complaining when you compile.

    1. What platform are you running?
    2. Use getchar() instead of getch()/getche()
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by ssharish2005 View Post
    First thing first. Your code donst even compile in the first place. The function your calling getch, getche etc are defined in conio.h which is a non standard library and therefore the compiler would start complaining when you compile.

    1. What platform are you running?
    2. Use getchar() instead of getch()/getche()
    If you're going to answer a question at least read it first. He says he's using MinGW on Windows10, and obviously the program compiles for him. The strangest thing here is fgetchar, which is really just fgetc(stdin) (the same as getchar except it's guaranteed to be a function and not a macro).

    @zolfaghar:
    ch should be an int as that's what all of these functions (and getche in particular) return.

    Anyway, it seems that your confusion is from getchar only responding after you hit Enter, so it reads whatever character you enter but leaves the newline which is then read by fgetchar. getch and getche respond when any key is pressed and don't wait for an entire line.

  4. #4
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by algorism View Post
    @zolfaghar:
    ch should be an int as that's what all of these functions (and getche in particular) return.

    Anyway, it seems that your confusion is from getchar only responding after you hit Enter, so it reads whatever character you enter but leaves the newline which is then read by fgetchar. getch and getche respond when any key is pressed and don't wait for an entire line.
    Thanks. Yes, that was the issue. I added a
    Code:
    fflush(stdin)
    to the line right after getche() and it worked.
    Here is the output...
    Code:
    $ second
    Press any key to continue
    Type any characterk
    Type any characterl
    Continue Y/NY
    Code:
    #include <stdio.h>
    int main ()
    {
    char ch;
    printf ("\nPress any key to continue");
    getch();
    printf ("\nType any character");
    ch = getche();
    printf("\nType any character");
    getchar();
    fflush(stdin);
    printf("\nContinue Y/N");
    fgetchar();
    }
    I am using an old version of Let us C. Folks have told me to switch to some other content, but MinGw on Windows seems to have the libraries, which I need to test the code in this book. So far I love it. The only thing is the editor. Although I have vim and I like it, the colors are all the same. Maybe I need to download some package I forgot to download. As far as letting me do what I need to, MinGw is great. I am so happy with it.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    getch, getche, and fgetchar should actually all begin with an underscore. And you should probably include conio.h for _getch and _getche. And note that fflush(stdin) is also non-standard (technically, undefined behaviour) and does not necessarily do what you want on other systems. It seems to work on windows, but a more compatible way to eat the newline is something like:
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF)
        ;
    Also, remember that ch should be an int, not a char. Look at the manual for those functions. They all return an int.

  6. #6
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Quote Originally Posted by algorism View Post
    getch, getche, and fgetchar should actually all begin with an underscore. And you should probably include conio.h for _getch and _getche. And note that fflush(stdin) is also non-standard (technically, undefined behaviour) and does not necessarily do what you want on other systems. It seems to work on windows, but a more compatible way to eat the newline is something like:
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF)
        ;
    Also, remember that ch should be an int, not a char. Look at the manual for those functions. They all return an int.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug a program that takes a large text file as input
    By arortell in forum C Programming
    Replies: 3
    Last Post: 11-07-2014, 04:39 PM
  2. Replies: 1
    Last Post: 07-19-2012, 08:52 PM
  3. input parameter debug
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 07-14-2008, 03:22 AM
  4. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  5. Function input may char or char[] ..?
    By GSalah in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2006, 04:07 AM

Tags for this Thread