The conversion specifier "%s"
breaks on whitespace.
If you enter, for instance, "John Smith", the variable author
will have "John"
and the rest of the input will be used for year
.
Always validate the return value of (most) library functions.
printf("Insert the name of the author to search: ");
if (scanf("%300s", author) != 1) /* error */;
printf("Insert the year: ");
if (scanf("%d", &year) != 1) /* error */;
The best way to get user input is to use fgets()
, then, if needed, parse the input and assign to variables.
char tmp[1000];
printf("Insert the name of the author to search: ");
if (!fgets(tmp, sizeof tmp, stdin)) /* error */;
strcpy(author, tmp);
printf("Insert the year: ");
if (!fgets(tmp, sizeof tmp, stdin)) /* error */;
if (sscanf(tmp, "%d", &year) != 1) /* error */;