Here is my code for a simple programme in C for storing details of students in a struct. The problem that I am having is that the fgets statement for taking in the name string doesn't get executed and directly prints my next printf statement The error line is marked. Also, the rest of the code works perfectly. Now I have tried the same code with changing 'fgets' to 'gets' and 'scanf'/'scanf_s' too, but I am having the same result.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Students
{
char name[30];
int age;
int roll_number;
char address[30];
}std[3];
void names(struct Students std[], int n)
{
int i = 0;
printf("Here are the names of students whose age is 14");
while (i < n)
{
if (std[i].age == 14)
{
printf("%s", std[i].name);
}
i++;
}
}
void everoll(struct Students std[], int n)
{
int i = 0;
printf("Here are the names of students whose age is 14");
while (i < n)
{
if (std[i].roll_number % 2 == 0)
{
printf("%s", std[i].name);
}
i++;
}
}
void details(struct Students std[], int n, int a)
{
int i = 0;
while (i < n)
{
if (std[i].roll_number == a)
{
printf("Here are the details\n");
printf("The name is : %s\nThe address is : %s\nThe age is : %d", std[i].name, std[i].address, std[i].age);
}
i++;
}
}
int main()
{
printf("Enter the details of the students:\n\n");
for (int i = 0; i < 3; i++)
{
printf("Enter the roll number of the student:\n");
scanf_s("%d", &std[i].roll_number);
printf("Enter the age of the student:\n");
scanf_s("%d", &std[i].age);
printf("Enter the name of the student:\n");
fgets(std[i].name, 30, stdin); //error line
printf("Enter the address of the student:\n"); //This line gets executed dirctly
fgets(std[i].address, 30, stdin); //This fgets works fine
}
eve:
printf("Which operation do you want to perform \n1) finding students with age 14\n2) Printing names of students with even roll number\n3) Finding details by enetering the roll number\n");
int d;
scanf_s("%d", &d);
int b;
switch (d)
{
case 1:
names(std, 3);
break;
case 2:
everoll(std, 3);
break;
case 3:
printf("Enter the roll number:\n");
scanf_s("%d", &b);
details(std, 3, b);
break;
default:
printf("Wrong number entered");
goto eve;
}
return 0;
}