I have this function:
Code:
void prompt(Line *head) {
char command[CMDLENGTH] = {};
int numScanned = 0;
int lineNumber = 1;
printf("? ");
numScanned = scanf("%s", command);
while (strcmp(command, "q") != 0
&& strcmp(command, "x") != 0
&& numScanned != 0) {
printf("command = %s\t", command);
if (strcmp(command, "h") == 0) {
helpCommand();
} else if (strcmp(command, "p") == EQUAL
|| strcmp(command, "+") == EQUAL
|| strcmp(command, "-") == EQUAL
|| strcmp(command, "\n") == EQUAL
|| (stringIsNum(command) == YES
&& stringToNum(command) > 0) ) {
lineNumber = printCommand(head, command, lineNumber);
} else {
printf("Unknown command: ignoring\n");
}
printf("? ");
numScanned = scanf("%s", command);
}
}
And the way input works is by prompting the user with a question mark, then the user enters some character or number and pressed enter. I was first using getchar and then flushing out the new line character, but then I realized that the user can enter a multiple digit number as well, so I scrapped getchar and replaced it with scanf.
Now I have a problem with scanf. While I want the new line characters after another character / number is entered to be flushed out, the user is supposed to be able to send enter as well and I'm meant to treat that as I would with the + character.
The problem is that it seems like scanf doesn't read new line characters. So what can I do to get around this problem?