Hello,

My program actually works but has bugs in it and I don't know why the bugs are showing up.

Background
I am teaching myself how to program in C. I have been studying this for a while in my spare time and have written a program to test out what what I can remember from the skills I have learned. This isn't for anything other than my own self satisfaction and testing my skills thus far.

I am programming in a Linux environment and am a beginner with gdb, linux, C and the GCC compiler. I cannot get gdb to get past any fgets functions I have in the program. It hangs every time it gets to fgets so gdb is pretty much useless to me in trying to figure this out. Even when I try to type something in and hit enter it doesn't work. I even push ENTER 500 times (dramatization but not far off) and that finally gets past it but then proceeds too far in to the program even using next function and I have tried to manually set the variable by using set x="Whatever" to no avail.

Issue
When running the program if you type in text longer than what fgets is expecting instead of dropping the excess text like I have seen it do before, the excess text is getting copied over to the next question and causing the next questions to be answered automatically by the excess text.

Example
What is the 1st employees first name:
Correct Answer: Kammi
Buggy Answer: Kammiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii i

Result
The correct answer is perfect. It will cause the next question to come up. The buggy answer will cause the unexpected result of the immediate question being answered with kammiiiiiiiiiiiiiii (19 characters because fgets is set to take in 20 characters including the NUL terminator '\0') however then it answers the next question which ends up as "What is Kammiiiiiiiiiiiiiii's last name? " as "iiiiiiiiiiiiiiiii" and continues this behavior until all of the iiiiiiii's have been used up.

Attempts
  • I have tried to use fflush(stdout) after the question to no avail.
  • I have attempted to look at the length of the string and clear out the string if it is over what is expected. If I use a printf("Length of String: %d",strlen(storage)); it prints out the string length I was expecting.
  • I remove the new line that fgets inserts automatically and replace it with a 0 already (if it finds one), I have attempted to set it to a NUL terminator instead.


Question
I have no idea what is causing this issue and need some help telling me what's wrong and how to fix it. Can someone please look over the code and tell me what's wrong with it?

Code
CodePaste.NET - Working code for using structures, arrays and pointers all together. - Every line has been commented.

GCC Compiling Command
gcc -ggdb -std=c99 -Wall -Werror ptr-strct.c -lcrypt -lcs50 -lm -o ptr-strct

Other Notes
There may be other commands not recognized by some people such as -lcs50 and also questions as to why I am using other settings such as -std=c99. This is because I am going through the Harvard classes and am using the CS50 appliance provided by Harvard.