Thread: Help with C code

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    15

    Help with C code

    I have the following code:
    Code:
    #include <stdio.h>
    #include <curses.h>
    
    void main(void)
    {
     char username[15],ch;
     int i=0;
     printf("\nUsername:");
    
     while(1)
    {
        ch = getch();
        if (ch == 10)
        {
        break;
        }
        else if (ch == 8 || ch == 127)
        {
         printf("\b\b");
        }
        else
        {
         username[i]=ch;
         i=i+1;
        };
    }
     printf("\nWelcome %s.\n",username);
    }
    If I compile this and run it with
    gcc mybbs.c -o mybbs -lncurses

    I get a segmentation fault and I isolated it to the line username[i]=ch;
    but I don't understand why. Also, the goal of the program is when a user presses the backspace key (code 8 and 127) on the keyboard in syncterm (a terminal program) I want it to simply erase the ^H thats displayed on the screen and send two backspaces with \b\b, can someone help please?

  2. #2
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by cnnx View Post
    I have the following code:
    Code:
    #include <stdio.h>
    #include <curses.h>
    
    void main(void)
    {
     char username[15],ch;
     int i=0;
     printf("\nUsername:");
    
     while(1)
    {
        ch = getch();
        if (ch == 10)
        {
        break;
        }
        else if (ch == 8 || ch == 127)
        {
         printf("\b\b");
        }
        else
        {
         username[i]=ch;
         i=i+1;
        };
    }
     printf("\nWelcome %s.\n",username);
    }
    If I compile this and run it with
    gcc mybbs.c -o mybbs -lncurses

    I get a segmentation fault and I isolated it to the line username[i]=ch;
    but I don't understand why. Also, the goal of the program is when a user presses the backspace key (code 8 and 127) on the keyboard in syncterm (a terminal program) I want it to simply erase the ^H thats displayed on the screen and send two backspaces with \b\b, can someone help please?
    You have created a version of what is known as a buffer overflow. Which is bad.

    The reason you get a segmentation fault is because you write outside of your array (buffer overflow). It can only hold 15 values, starting at username[0] up to username[14]. You have to make sure that you do not write outside of that.

    EDIT: getch returns an int, so ch should be declared as int and you should handle potential errors from getch. Might I inquire what problem it is that you really are trying to solve and why?
    Last edited by Jimmy; 01-17-2015 at 07:11 PM. Reason: I'm tired...

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    15
    I've added the check, it no longer does a segmentation fault but also doesn't print the username with the printf statement. Is this because I'm using curses.h?

    Code:
    #include <stdio.h>
    #include <curses.h>
    
    void main(void)
    {
     char username[15],ch;
     int i=0;
     printf("\nUsername:");
    
     while(1)
    {
        ch = getch();
        if (ch == 10)
        {
        break;
        }
        else if (ch == 8 || ch == 127)
        {
         printf("\b\b");
        }
        else
        {
         if (i <= 15 )
         {
         username[i]=ch;
         i=i+1;
         };
        };
    }
     printf("\nWelcome %s.\n",username);
    }

  4. #4
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    It is still wrong though, read my post again. Which values are valid indices for you array?

    Also, what problem are you trying to solve? For example, why are you using getch to read your input? Why are you checking for ASCII value 8 and 127? Why is ch still a char?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    1. main returns int, not void

    2. if (i <= 15 ) should be <. You also need to explictly store a \0 to mark the end of the string.

    3. printf("\b\b"); don't you need to decrement your loop counter as well?

    > but also doesn't print the username with the printf statement.
    That depends. Do you see "welcome" and then nothing, or does the whole window suddenly disappear because the program ended (see the FAQ for how to prevent this).
    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.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And why use a non standard function getch() which is was orginally defined in conio.h in turboc. Instead use getchar() which is defined in stdio.h. Which its functionality is similar to getch
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    Jan 2015
    Posts
    15
    Code:
    #include <stdio.h>
    
    int main(void)
    {
     char username[16],ch;
     int i=0;
     printf("\nUsername:");
    
     while(1)
    {
        ch = getchar();
        if (ch == 10)
        {
        break;
        }
        else if (ch == 8 || ch == 127)
        {
         printf("\b\b");
         i--;
        }
        else
        {
         if (i <= 14 )
         {
         username[i]=ch;
         i=i+1;
         };
        };
    }
     username[i+1]='\0';
     printf("\nWelcome %s.\n",username);
    }
    I made some changes based on your inputs, the goal is for this program to recognize the backspace keypress in syncterm and actually erase the last key instead of printing ^H on the screen. It is currently not working, it shows ^H on the screen.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Perhaps what you need this...

    Code:
             if (i <= 14 )
             {
                username[i]=ch;
                i++;
             }
    
    my output:
    cprogramming$ ./a.out 
    
    Username:ShouldIWork?
    
    Welcome ShouldIWork?.
    Try that and validate behaviour?
    Last edited by ssharish2005; 01-17-2015 at 08:17 PM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    My suggestion then is to configure your terminal correctly instead.

  10. #10
    Registered User
    Join Date
    Jan 2015
    Posts
    15
    Local console is fine, its via syncterm the backspace key is not being intercepted and replaced by backspace.

  11. #11
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by cnnx View Post
    Local console is fine, its via syncterm the backspace key is not being intercepted and replaced by backspace.
    Yes, so again, fix your console so that it works with syncterm or switch software to something that works out of the box.

    This is a configuration issue. I don't even understand how you think the program you are writing are going to help with the real issue you have?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM