Basicaly I'm trying to do this

int create_student(student_data *student){
	printf("Nome do aluno:\n");
	fgets(student->name,name_limit,stdin); //wtf?
	printf("NĂºmero do aluno\n");
	printf("Aqui devia estar um nome %s",student->name);
	return 1;

Where student is a custom struct, student->name is char array. For some reason it seems to skip the fgets. Replacing with gets has no effect but scanf does seem to work.

If you think the problem lies with something else in my code I can post it. I just don't want to scare people away :P

Edit: Recently replaced number with a char array and the second fgets does seem to work. Just not the first one.

10 Years
Discussion Span
Last Post by Dave Sinkula

The 'skipping' is likely an effect of taking input elsewhere and leaving a newline in the stdin. So I would tend to think your issue is in other code.


Wouldn't fflush(stdin) solve it though? Is there a way I can clear it if that doesn't work?


If you take all input as a string using fgets, you can pretty much eliminate the "need" to fflush(stdin). (Which is what I'd said in the tutorial -- although I think some parts were trimmed from it.)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.