I was unsure of how to ignore the enter, so I've done it in a crude way
Code:
void prompt() {
char command = '\0';
int numScanned = 0;
printf("? ");
numScanned = scanf("%c", &command);
printf("command = %c\n", command);
while (numScanned == 1) {
if (command == 'h') {
helpCommand();
}
printf("? ");
while (command == '\n' || command == ' ' || command == '\t') {
numScanned = scanf("%c", &command);
}
numScanned = scanf("%c", &command);
}
}
Yet it's still not working, and I'm already seeing a problem with this because later on I'm going to need to accept a newline from the user (as opposed to h or any other character I'll also be using). Also, I'm unsure with what other characters can be used besides new lines, spaces and tabs.
EDIT: What I've written clearly doesn't work either. It scans until it finds something other than those blank characters and then it waits to scan again.
EDIT2: Ok I have it working:
Code:
void prompt() {
char command = '\0';
int numScanned = 0;
printf("? ");
numScanned = scanf("%c", &command);
printf("command = %c\n", command);
while (numScanned == 1) {
if (command == 'h') {
helpCommand();
}
printf("? ");
numScanned = scanf("%c", &command);
while (command == '\n' || command == ' ' || command == '\t') {
numScanned = scanf("%c", &command);
}
}
}
I still have the problem that I'll need to accept a newline character from the user at some point, but I guess I can cross that bridge when I get there.
Also, is it possible to make this more succinct? I find it hard to believe that I need 3 scanfs to just do the one task of scanning.