I should mention that this does work in a different environment: in our programming class we normally use the IDE codeblocks (which is find awful), so I just use the gcc compiler and vim in my terminal (I'm on arch). I didn't encounter problems until recently, when I had to read in a string which contained spaces. For that I thought using the fgets()
function would be a good idea, but it created some problems. This is what the code looks like:
void addStudent() {
struct Student student;
printf("Name of student: ");
fgets(student.name, 25, stdin);
}
This however does not prompt me for input in my shell, it simply continues and reads in a newline character \n
immediately. Do you guys have any idea how to fix this?
As pointed out by the comments, it's not good to combine a function like scanf()
with fgets()
. When scanf()
is called it leaves a newline character in the input buffer which is then immediately read by fgets()
, causing it to fail prompting the user.