+ 2

Plz Check my program it s not working well

#include<stdio.h> typedef struct { char name[50]; char last_name[50]; int age; }person ; int main(){ person T[10]; int i; for(i=0;i<2;i++){ printf("enter the informations of the person %d\n",i+1); puts("Enter the first name"); gets(T[i].name); puts("Enter the family name"); gets(T[i].last_name); printf("Enter the age"); scanf("%d",&T[i].age);} for(i=0;i<2;i++){ printf("the informations of the person %d are\n",i+1); puts(T[i].name); puts(T[i].last_name); printf("%d",T[i].age);}}

18th May 2017, 11:01 PM
Zineb Ab
Zineb Ab - avatar
6 Réponses
+ 9
you must use fflush(stdin); at the end of the for loop. #include<stdio.h> typedef struct { char name[50]; char last_name[50]; int age; }person ; int main(){ person T[10]; int i; for(i=0;i<2;i++){ printf("enter the informations of the person %d\n",i+1); puts("Enter the first name"); gets(T[i].name); puts("Enter the family name"); gets(T[i].last_name); printf("Enter the age"); scanf("%d",&T[i].age); fflush(stdin); // this what you miss } for(i=0;i<2;i++){ printf("the informations of the person %d are\n",i+1); puts(T[i].name); puts(T[i].last_name); printf("%d",T[i].age); } }
19th May 2017, 12:11 AM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 9
i have tryied and it is working well.. did you put it after scanf()??
19th May 2017, 1:47 AM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 9
there is another method you can use put ins instead of "fflush(stdin);" the below: while( getchar() != '\n'); it must work. for(i=0;i<2;i++){ printf("enter the informations of the person %d\n",i+1); puts("Enter the first name"); gets(T[i].name); puts("Enter the family name"); gets(T[i].last_name); printf("Enter the age"); scanf("%d",&T[i].age); while(getchar() != '\n'); }
19th May 2017, 1:59 AM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 9
welcome..😉
19th May 2017, 3:08 PM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 1
It still not working yet :/ .. the program demand the informations for the first time .. everything is gd .. the problem is when it asks for the informations for the second time .. the prog doesn't demand to enter the name .. it moves directly to enter the family name
19th May 2017, 12:36 AM
Zineb Ab
Zineb Ab - avatar
+ 1
got it .. thank uu 😊😊
19th May 2017, 12:35 PM
Zineb Ab
Zineb Ab - avatar