Every time I try to use an array of structs and get the input from the users it skips a scanf and I can't figure out why. I simplified the code by making the array of only 1 element and scanning just for that element and not with a for loop, but it still doesn't work.
#include <stdio.h>
#include <string.h>
int main(){
typedef struct {
char title[30];
char author[30];
int year;
} Books;
Books library[1]; //array of structs
//input
printf("\nAdd a new book to the shelf");
printf("\nTitle: ");
scanf("%[^\n]",library[0].title);
printf("\nAuthor: ");
scanf("%[^\n]",library[0].author);
printf("\nYear: ");
scanf("%d",&library[0].year);
//print
printf("\nTitle: ");
printf("%s",library[0].title);
printf("\nAuthor: ");
printf("%s",library[0].author);
printf("\nYear: ");
printf("%d\n",library[0].year);
return 0;
}
terminal:
Add a new book to the shelf
Title: Fist Book //input by user
Author: //doesn't let me scan anything and jumps to Year:
Year: 1998 //input
Title: Fist Book
Author:
Year: 1998