I heve a following code
while ( a != 5)
scanf("%s", buffer);
This works well but takes no space in between the mentioned words or in other words, scanf terminates if we use spaces to scan
If I use this
while( a != 5)
scanf("%[^\n]", buffer);
It works only for once which is bad
I never use gets()
because I know how much nasty it is..
My last option is this
while( a != 5)
fgets(buffer, sizeof(buffer), stdin);
So my questions are
- Why the second command is not working inside the loop?
- What are the other options I have to scan a string with spaces?
"%[^\n]"
will attempt to scan everything until a newline. The next character in the input would be the\n
so you should skip over it to get to the next line.Try:
"%[^\n]%*c"
, the%*c
will discard the next character, which is the newline char.