I want the following to ask for input and then take in a string (with spaces), then do it again. But it repeatedly outputs "input$" after typing in the first string.
char command[80];
while(1)
{
printf("input$ ");
scanf("%[^\n]", command);
}
My output: nput$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$^C
What I want:
input$ hi
input$ this can take spaces
input$
scanf. c-faq.com/stdio/scanfprobs.htmlscanf()reports that it was not able to convert anything (return value 0, not EOF), but you're blithely ignoring it. You have to read the newline (getchar(), perhaps) to allow it to continue. Or add a\nafter the]; or, indeed, a space would do. If you don't care about leading spaces, a space before the%would work, too. It is incredibly hard to use thescanf()functions correctly; it is really 'cruel and unusual punishment' to make beginners use them.