New course! Every coder should learn Generative AI!
Try a free lesson+ 2
In your scanf() calls, put an extra whitespace after the format specifier:
scanf( "%d ", ...);
^---
Right now, fgets() is blocked by the newline character entered after a number, which remains in the input buffer. Adding that whitespace will have scanf() consume all additional whitespace after the number, which is turn means fgets() will not immediately stop reading characters because it encounters a whitespace character first.
+ 2
Yes.
+ 2
In that case, don't do what I said earlier, but insert a single
scanf( " " );
^--- space
right before the call to fgets() instead. That should still take care of the whitespace issue, while properly displaying the printf() messages (well, it at least does on my terminal).
+ 2
That sounds weird. I'll have to look into it later though, after getting some sleep. Can you update your code in the description to reflect the current version?
+ 2
Oh, you simply switched fgets() and scanf(). This is what I meant:
...
scanf(" ");
fgets((ptr+i)->name, sizeof((ptr+i)->name), stdin);
...
+ 2
As far as I can see, the ':' is there because you printed it yourself:
printf("\nName -> %s: ", (ptr+i)->name);
So you could just change the output to:
printf("\nName -> %s", (ptr+i)->name);
Note fgets() stores the final newline character in the string, that is why the ':' appears in a new line. If you also want to get rid of that, you'll have to do so manually.