0

I know its probably a stupid question but I’m learning and I need help. Can someone explain to me why does my program stops getting input from the console after the first book.

#include <stdio.h>
#include <stdlib.h>

typedef struct book
{
    char title[50];
    char author[50];
    int year;
    int price;
}Book;

Book getInfo()
{
    Book b1;
    scanf("%[^\n]%*c",b1.title);
    scanf("%[^\n]%*c",b1.author);
    scanf("%d",&b1.year);
    scanf("%d",&b1.price);
    
    return b1;
}


int main()
{
    Book books[2];
    
    for(int i=0;i<2;i++)
    {
        books[i]=getInfo();
    }
    
    printf("%s-%s-%d-%d",books[0].title,books[0].author,books[0].year,books[0].price);
    
    return 0;
}
urlooman
  • 1
  • 2
  • 1
    After the price is entered, the newline is left in the buffer. That stops the next title input from working. Use `fgets()` to read lines and `sscanf()` to parse results. – Jonathan Leffler Apr 19 '22 at 12:49

0 Answers0