0
#include<stdio.h>  
#include<string.h>

 struct userinput{
    int code;
    float salary;
    char name[30]
 };

int main(){
   struct userinput u[5];   
   int i;
   for(i=0;i<5;i++){
      printf("Enter name of student %d", i+1);
      fgets(u[i].name, 30,stdin);
      printf("Enter your code");
      scanf("%d", &u[i].code);
      printf("Enter your salary");
      scanf("%.2f", &u[i].salary);
      }
   for(i=0;i<5;i++){
      printf("your code is:-%d", u[i].code);
      printf("your salary is:-%f", u[i].salary);
      printf("your name is:-%s", u[i].name);
      }
    return 0;
}

I tried to enter details of 5 people in structure using loop. But when I tried to input the values after the first input of name the next iteration simply skips the name portion and directly moves to code and salary.

Victor
  • 95
  • 2
  • 8
  • `scanf` leaves a trailing newline character which the next `fgets` call will immediately read as an empty line. See duplicate post for more details and fix suggestions. – kaylum Apr 01 '22 at 06:23

0 Answers0