Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So last option is to post here else I dunno what to do,

I have this menu system for a Linux test program, C program. Lots of options and initially I had a scanf that worked fine. Then scanf stopped working, so I guess there were a lot of white spaces and new lines etc preventing me from entering an input ( Don't particularly understand that, because the first actual output to the screen/stdout is this menu system upon program start up).

Anyway, I then tried another option, which also worked for a while. The program when it displayed the menu system paused for a bit allowed me to enter an option and continued on its merry way..... AAAAAAannnnnd then stopped working resulting in segmentation fault error, using GDB if you don't enter any value it pops up with this seg fault error. Don't particularly understand how in one instance it will pause and allow me enter stuff and the next goes straight through and Seg fault.

Any Ideas as to why it worked and now stopped working?? Even if any of you do offer any solutions I think its only a matter of time when that breaks as well :(... So lost ... Any help greatly appreciated .

The code below is a copy n paste from a website,

C++
char choice;

printf("\t\t\tMenu Title\n");
printf("Menu Item1\n");
printf("Menu Item2\n");
printf("Menu Item3\n");
printf("Menu Item4\n");
printf("Menu Item5\n");

choice = *(read_line());

//read Line function defined as follws
char *read_line (void)
  /**** Read at most 'length'-1 characters from the file 'f' into
        'buf' and zero-terminate this character sequence. If the
        line contains more characters, discard the rest.
   */
{
  char *p;
  char buf[100] = {0};
  int length = 100;
	
  if (p = fgets (buf, length, stdin)) {
    size_t last = strlen (buf) - 1;

    if (buf[last] == '\n') {
      /**** Discard the trailing newline */
      buf[last] = '\0';
    } else {
      /**** There's no newline in the buffer, therefore there must be
            more characters on that line: discard them!
       */
      fscanf (stdin, "%*[^\n]");
      /**** And also discard the newline... */
      (void) fgetc (stdin);
    } /* end if */
  } /* end if */
  return p;
} /* end read_line */
Posted
Comments
Richard MacCutchan 15-Nov-13 9:34am    
This code will not even compile. And even if you change it so it does, your read_line function is returning a pointer to a stack buffer which will no longer exist after the return.
codenameyash 15-Nov-13 9:48am    
Point noted good sir, making some adjustments
Albert Holguin 15-Nov-13 11:34am    
You probably shouldn't be reading a line with an fgets() since that gives you a set buffer size from a file and not a line from a file (big difference)... you're logic is not very good for a read_line() function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900