0

I made a structure and tried obtaining the name using gets(). It worked for the first iteration but accepted nothing for the next iterations.

#include <stdio.h>

struct student{
    int rollno;
    char name[15];
    struct {
        int sub1;
        int sub2;
        int sub3;
    }marks;
}fs[3];

int main(){
    int marks, sub1, sub2, sub3, x, y, count;
    struct student temp;
    for (x=0; x<3; x++){
        printf("\nEnter details of the student %d:\n", x+1);
        printf("Please Enter your name: ");
        gets(fs[x].name);
        printf("Please Enter your roll no: ");
        scanf("%d", &fs[x].rollno);
        printf("Please Enter your marks for sub 1: ");
        scanf("%d", &fs[x].marks.sub1);
        printf("Please Enter your marks for sub 2: ");
        scanf("%d", &fs[x].marks.sub2);
        printf("Please Enter your marks for sub 3: ");
        scanf("%d", &fs[x].marks.sub3);
    }

The output is something like this. I don't understand if it works for the first time why does it fail for the second iteration itself.

Enter details of the student 1:
Please Enter your name: Louis
Please Enter your roll no: 12
Please Enter your marks for sub 1: 87
Please Enter your marks for sub 2: 79
Please Enter your marks for sub 3: 83

Enter details of the student 2:
Please Enter your name: Please Enter your roll no: 4
Please Enter your marks for sub 1: 5
Please Enter your marks for sub 2: 3
Please Enter your marks for sub 3: 2

Enter details of the student 3:
Please Enter your name: Please Enter your roll no: 54
Please Enter your marks for sub 1: 32 
Please Enter your marks for sub 2: 45
Please Enter your marks for sub 3: 34
Total & Average of each student.

The names for the next iterations are taken to ' '. Moreover can any other method be used as I am very new to C.

0 Answers0