+ 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);}}
6 ответов
+ 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);
}
}
+ 9
i have tryied and it is working well.. did you put it after scanf()??
+ 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');
}
+ 9
welcome..😉
+ 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
+ 1
got it .. thank uu 😊😊