I have some functions like push, pop, delete etc. for a singly linked list and implemented the following function to get user input:
void user_input(){
char input[10];
while(fgets(input, 9, stdin)){
if(strncmp(input, "add", 3) == 0){
int x;
printf("Number to add: ");
scanf("%d", &x);
push(x);
printf("%d added.\n", x);
}
else if(strncmp(input, "del", 3) == 0){
int x;
printf("Number to delete: ");
scanf("%d", &x);
delete(x);
printf("%d deleted.\n", x);
}
else if(strncmp(input, "q", 1) == 0){
printf("Program terminated.\n");
break;
}
// and some other if else statements...
}
So I can input a string like "add", then strncmp will compare it and I get another prompt asking me to enter the number I want to add and stores in x using scanf. Something like this:
add
Number to add: 5
5 added.
However I am looking for a way to be able to enter something like this:
add 5
del 2
etc.
Basically a string and int value in one line separated by space, instead of writing "add" first, pressing enter and writing the number. I tried using sscanf but had no luck yet.