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$
You normally want to use something like:
The ’79’ prevents a buffer overflow, and the
%*cconsumes the new-line from the input buffer. It has one minor shortcoming: it will still consume (and throw away) a character, even if the next character in the input buffer is not a new-line. If you have to deal with that possibility, you can read it and ignore it unless yourcommandbuffer is full: