Problem in reading inputs
Hi,
The following code is not working since fgets read the newline character when enter is pressed......
I have used fflush but this not working , can anybody let me know the correct way ?
Code:
#include<stdio.h>
#include<string.h>
struct student
{
int roll_no;
char name[25];
char city[25];
char course[10];
};
int main()
{
int number, i;
struct student s[22];
printf("\n STUDENT RECORD PROGRAME");
printf("\nEnter The No. Of Records to Enter");
scanf("%d",&number);
printf("\nEnter The Data Of students");
for(i= 1;i<=number; i++) {
printf("\nEnter Name:");
fflush(stdin);
fgets(s[i].name, sizeof(s[i].name), stdin);
printf("\nEnter Roll No.:");
scanf("%d",&s[i].roll_no);
printf("\nEnter City:");
fgets(s[i].city, sizeof(s[i].city), stdin);
printf("\nEnter Course:");
fgets(s[i].course, sizeof(s[i].course), stdin);
}
printf( "\n The Details Of Student Are:\n");
for(i= 1;i<=number; i++) {
printf("\n Enter Name: %s",s[i].name);
printf("\nRoll No.:%d",s[i].roll_no);
printf("\n City:%s",s[i].city);
printf("\n Course:%s",s[i].course);
}
}
Thanks