给我做了一个我似乎无法理解的练习。我几乎完成了我的作业,但我被困在这个功能上。
限制: 只能有 10 个唯一的学生 ID。 有5个学科领域的研究。 一个学生只能选修 2 门科目。
我的 struct.h 看起来像这样:
typedef struct student_info{
int student_id;
int course_id[2];
}student;
在main.c中
student info[10];
在 func.c 中
假设我提示用户输入学生 ID。
printf("Enter Student ID. ");
scanf("%d", &info->student[count_stud]->student_id;
用户输入123
然后提示用户输入类(class) ID。
printf("Enter Course ID. ");
scanf("%d", &info->student->course_id[count_cour];
用户输入101
我的问题在于打印出特定的student_id 和学生正在学习的类(class)。还使用 for 循环我找不到找到重复项的方法。我可以找到用户最后输入的 ID,但是当我输入之前 2 个输入中的 ID 时,它会传递我的 if else 语句。
感谢任何帮助。
最佳答案
student info[10];
此处,info
是 10 名学生
的数组
,因此您必须使用索引来读取它。
for(int student_count = 0; student_count < 10; student_count ++)
{
printf("Enter Course ID 1 for student %d. ",student_count+1);
scanf("%d", &info[student_count].course_id[0]);
printf("Enter Course ID 2 for student %d. ",student_count+1);
scanf("%d", &info[student_count].course_id[1]);
}
关于C 编程、结构体和多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057987/