I have a code here which has some unwanted behaviour when inputting strings with spaces. eg print illegal_argument
. When an input with multiple words comes in I would like it to only register the first word.
What currently happens is:
christian@oblig2 $ print 0
Enter router ID for information > ID: 0, flag: f7, model_length: 13, model: D-Link y6000
It registers the first print
for the comparison in the command-loop. But instead of scanf
prompting the user for the next input, it instead ignores that and takes the second word/number input by the user from the previous scanf
.
What I want to happen is:
christian@oblig2 $ print 0
Enter router ID for information > 0
ID: 0, flag: f7, model_length: 13, model: D-link y6000.
Everything works as intended otherwise, but I wonder if there's a simpler way to do this. I've tried using Strtok, without much luck.
char input[256];
printf("%s@oblig2 $ ", getenv("USER"));
scanf("%s", input);
if(strcmp(input, "print") == 0) {
print();
}
The above code is how I get input from the user.
Anyone got any ideas?