I am building a program, which needs to read input from user in form of command such as 'command 12' where command is specific word and needs to be stored in separate variable, number after it needs to be in its variable too.
I created variable in which is word that needs to match the command and int variable for storing.
char rule[5] = {'r', 'u', 'l', 'e', '\0'};
char command[6];
int cmd_num;
And then used scanf to read it and then compare it by strcmp.
scanf("%s %d", &command, &cmd_num);
if ( (strcmp(command, rule)) != 0)
{
printf("Error.\n");
return 1;
}
Yet it doesn't work as I would expect it. I googled for hours, but I am not capable of finding how to properly store this string and number after it. Could anyone help me to understand how string is stored? It would help me to understand why strcmp doesn't work the way I'd expect it.
Or is there any better way to check if user input is matching certain word? The number after it seems to be problem for me. I tried to play around with gets functions but I was not able to come to results. Thank you in advance.