Hey C Board. I created a typedef structure called Student, and I want to pass this structure to two functions, getStudent, to scanf all the members, and printStudent, to print all the members.
Code:
#include <stdio.h>
typedef struct {
char name[70];
int age;
double gpa;
} Student;
void getStudent(Student *s) {
printf("Enter name: ");
fgets(s->name, sizeof(s->name), stdin);
printf("Enter age: ");
scanf("%d", &s->age);
printf("Enter GPA: ");
scanf("%lf", &s->gpa);
}
void printStudent(Student *s) {
printf("Name: %s", s->name);
printf("Age: %d\n", s->age);
printf("GPA: %.2f\n", s->gpa);
}
int main(void) {
Student bill, jeff;
getStudent(&bill);
printf("\n");
printStudent(&bill);
printf("\n");
getStudent(&jeff);
printf("\n");
printStudent(&jeff);
return 0;
}
Here's the output. It completely skips the second time I call fgets. What am I doing wrong?
Code:
$ ./a.out
Enter name: James
Enter age: 20
Enter GPA: 3.4
Name: James
Age: 20
GPA: 3.40
Enter name: Enter age: 22
Enter GPA: 4.0
Name:
Age: 22
GPA: 4.00