Error reading string in C

Asked

Viewed 55 times

-1

Good afternoon people! In my program I use for reading some strings that fill a struct vector. The first reading usually occurs, but in the second reading the program skips the reading of a string, I think the error may be in the scanf, I really appreciate if you can help me.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Change the comma in 1452.32 for a dot: 1452.32.

  • worked out! thank you very much!!

1 answer

0

Hello! What is happening is that the input formatter %[^\n] does not save your enter typed, but it is still captured by your keyboard buffer. This buffer gets "polluted" and interferes with reading the next string. One suggestion to get around this is to clear your buffer after a read.

printf("Digite o nome do funcionário: ");
scanf("%s", nome);
__fpurge(stdin);

Note: Remembering that __purge(stdin) is for Unix and fflush(stdin) is for windows

Browser other questions tagged

You are not signed in. Login or sign up in order to post.