C program with struct and fget

1078 views c

I want to make a program with struct which prints out the data of a film entered by the user. The program looks like this:

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

struct films {
char title[100];
int length;
char genre[100];

int main()
int n;

struct films film[100];
for (int i=0; i<n; i++){
printf("%d. film:\n", i+1);
printf("Title: ");
printf("Lenght: ");
printf("Mufaj: ");
printf("The films: \n");
 for (int i=0; i<n; i++)
    printf("The %d.film:[%s:%d:%s] ", i+1 , film[i].title, film[i].length, 
return 0;

I know i made a mistake at fget, but i don't know what should I do to make it right. Can somebody help me?

answered question

Hello Tyler please provide some more explanation about what does not work or what errors occur with the exact error message texts. Otherwise this is a "Look at my code and fix it" post instead of "help me to resolve this specific problem i can not figure out"

1 Answer


This can happen if you mix fgets with scanf.

After the scanf call to read the length, there's a newline left in the input buffer. On the following call to fgets, it reads just that newline.

Replace the scanf call with a call to fgets using a temporary string followed by sscanf:

char line[100];
fgets(line, 100, stdin);
sscanf(line, "%d",&film[i].length);

posted this

Have an answer?


Please login first before posting an answer.