我使用结构创建了一个程序,用于创建学生档案(及其成绩)。
我使用gets_s
作为输入。当第一个学生必须填写时,一切都很顺利。然而,当第二个,第三个......学生填写问题时,它会跳过第一个输入询问他们的名字。
为什么?
这是一个示例输出: https://gyazo.com/78de2c20e48b4f7fc095e5568176f79c
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_LENGHT 81
struct student new_student();
void print_student(const struct student *student);
struct student {
char name[MAX_NAME_LENGHT];
char first_name[MAX_NAME_LENGHT];
int studentid;
int year;
double grades[5];
};
void main() {
struct student student1, student2;
student1 = new_student();
student2 = new_student();
print_student(&student1);
print_student(&student2);
}
struct student new_student() {
struct student student;
char buffer[MAX_NAME_LENGHT];
float score;
printf("give your last name: "); //gets lastname
gets_s(student.name, sizeof(student.name));
printf("give your first name: "); //gets firstname
gets_s(student.first_name, sizeof(student.first_name));
printf("give your studentnumber: "); //gets studentid
gets_s(buffer, sizeof(buffer));
student.studentid = atoi(buffer);
printf("give your last completed year: "); // adds years
scanf_s("%i", &student.year);
for (int i = 1; i <= student.year; ++i) { //get grades per year
printf("give your score of your %ie year: ", i);
scanf("%f", &score);
student.grades[i - 1] = score;
}
printf("\n");
return student;
}
void print_student(const struct student *student) {
printf("\nschool: \n");
printf("\nName: %s", student->name);
printf("\nFirst Name: %s", student->first_name);
printf("\nstudentennummer: %i", student->studentid);
printf("\nyear: %i", student->year);
for (int i = 0; i<student->year; ++i)
printf("\ngrades: %f", student->grades[i]);
printf("\n");
}