0

Why My C program is not working properly

#include <stdio.h> typedef struct students { char name[20]; int id; } s1; int main(){ s1 detail[3]; for(int i=0;i < 3;i++){ printf("Enter Student %d Name : ",i+1); gets(detail[i].name); printf("Enter Student %d id : ",i+1); scanf("%d",&detail[i].id); } for(int i=0;i < 3;i++){ printf("Student %d Name is : %s",i+1, detail[i].name); printf("Student %d id is : %d\n",i+1, detail[i].id); } return 0; } // In this program when you get second or third time name or any character it will not take why // When I take char array with scanf it works but with scanf we can't take spaces or full name // When you take only character array in for loops gets function working properly // Why this function is not working in when I use multiple data type // Please tell me

10th Apr 2020, 11:00 AM
Yogesh Jangid
Yogesh Jangid - avatar
1 ответ
+ 1
1. Wrong header filename #include <stdio.h> // #include <studio.h> 2. Use `fgets` instead of `gets` fgets(detail[i].name, 20, stdin); // gets(detail[i].name); 3. Add `getchar();` after reading student's ID. This will consume the new line character '\n' that was left behind in the input stream after reading the student's ID. scanf("%d",&detail[i].id); getchar();
10th Apr 2020, 11:23 AM
Ipang