I have the following code:
If I compile this and run it withCode:#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); }
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?