-1
#include <stdio.h>
#include <string.h>
main()
{
    int i;
    struct name {
        char name[30];
        int roll_no;
        float marks;
    } s[3];
    for (i = 0; i < 3; i++) {

        printf("\nName: ");
        gets(s[i].name);
        printf("\nRoll number: ");
        scanf("%d", &s[i].roll_no);
        printf("\nMarks: ");
        scanf("%f", &s[i].marks);
    }

    for (i = 0; i < 3; i++) {
        printf("\nName:");
        puts(s[i].name);
        printf("\nRoll number: %d", s[i].roll_no);
        printf("\nMarks: %f", s[i].marks);
    }
}

please help in resolving the error in entering name. name gets entered once but after entering once it can't be entered again

DarthQuack
  • 1,254
  • 3
  • 12
  • 22
  • first *gets* is dangerous and must not be used. Second it is not executed one time, it just don't stop when reading a newline from previous input – bruno Jan 03 '21 at 10:07
  • Please see [fgets() doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf). Don't mix your input methods: use *one* method and stick to it. – Weather Vane Jan 03 '21 at 10:10
  • Does this answer your question? [c - how gets() work after scanf?](https://stackoverflow.com/questions/46104371/c-how-gets-work-after-scanf) – IrAM Jan 03 '21 at 12:39

1 Answers1

0

You can use fgets(string,length_of_string,stdin); or you can use scanf (" %s",....);

You should never use gets ,please read this :Why is the gets function so dangerous that it should not be used?

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

main()
{
  int i;

  struct name
  {
      char name[30];
      int roll_no;
      float marks;
  }s[3];

  for(i=0;i<3;i++)
  {
       printf("\nName: ");
       scanf(" %s",s[i].name);
       printf("\nRoll number: ");
       scanf("%d",&s[i].roll_no);
       printf("\nMarks: ");
       scanf("%f",&s[i].marks);
  }


  for(i=0;i<3;i++)
  {
       printf("\nRoll number: %d",s[i].roll_no);
       printf("\nMarks: %f",s[i].marks);
       printf("\nName:");
       puts(s[i].name);
  }

}
MED LDN
  • 684
  • 1
  • 5
  • 10