2

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.

Pernick
  • 33
  • 2
  • 9

1 Answers1

2

First, you can directly define rule as string using double quotes (and to avoid to write into it, make it a constant pointer):

const char *rule = "rule";
char command[6];
int cmd_num;

When you use scanf, limit length to read to avoid buffer overruns (using %5s which is the maximum size of the array minus zero-terminator), and don't pass address for arrays/pointers, because scanf can already access the address of the string to write into:

nb_items = scanf("%5s %d", command, &cmd_num);

Note that you can know how many items have been scanned (declare int nb_items;). For instance, if the user forgets to input a number or inputs a non-number, you'll get nb_items==1 and you'll know that cmd_num hasn't been initialized.

The main issue here is that you had undefined behaviour trying to store the result of your scanf in the address of the address of the array.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I tried to use limit for scanf (%5s), the problem is, that I have two commands, I dont know which one the user will type, and I need to recognize them both and unfortunately, they are not the same length – Pernick Nov 28 '16 at 20:45
  • then your `command` buffer is too small. Increase it to the biggest command length. Not checking your buffer could crash your program. 5 is the maximum limit. You don't have to enter 5 chars. – Jean-François Fabre Nov 28 '16 at 20:46
  • I tried to, but then strcmp doesn't work, could not it be because the strings arent equal? Lets say my input is 'raven 12' and I have const char *raven = "raven"; When I use strcmp on those two, it doesn't work, eventhough when I print them, they print the same string. – Pernick Nov 28 '16 at 20:55
  • Oh my god, my if statement was wrong, your answer is incredibly helpful, thank you very much! – Pernick Nov 28 '16 at 21:05