struct - Scanf needs more values than it should in C -


i'm trying learn structs , i'm using code

#include <stdio.h>  struct elements {     char name[50];     int semester;     char  am[15]; }student[100];  void read_struct(struct elements p[]); int i=0;  main() {      (i=0;i<2;i++)     {         read_struct(student);      }     (i=0;i<2;i++)     {   printf("%d\n",i);         printf("%s\n",student[i].name);         printf("%s\n",student[i].am);         printf("%d\n",student[i].semester);     }      system("pause"); } void read_struct(struct elements p[]) {      gets(student[i].name);     gets(student[i].am);     scanf("%d\n",&student[i].semester); } 

and face following problem: during second iteration when enter value variable student[1].semester program doesn't print i've entered waits me enter number,press enter , prints. tried fflush(stdin) after every gets , scanf, , got same problem.

try replacing

scanf("%d\n", &student[i].semester); 

with

scanf("%d", &student[i].semester); 

in addition that, fflush(stdin) invokes undefined behaviour, don't use it.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -