C编程,结构和多维数组

时间:2022-02-19 21:33:47

I am given an exercise that I can't seem to understand. I am almost done with my assignment but I'm stuck on this function.

我接受了一项我似乎无法理解的运动。我几乎完成了我的任务,但我坚持这个功能。

Limitations: There can only be 10 unique student ID's. There are 5 subject area of study. An a student can only take 2 subjects.

限制:只能有10个唯一的学生ID。有5个学科领域。学生只能参加2门科目。

My struct.h look like this:

我的struct.h看起来像这样:

typedef struct student_info{
  int student_id;
  int course_id[2];
  }student;

In main.c

在main.c

student info[10];

In func.c

在func.c中

Say I Prompt the user for a Student ID.

说我提示用户输入学生证。

printf("Enter Student ID. ");
scanf("%d", &info->student[count_stud]->student_id;

User inputs 123

用户输入123

Then Prompt the user for a course ID.

然后提示用户输入课程ID。

printf("Enter Course ID. ");
scanf("%d", &info->student->course_id[count_cour];

User inputs 101

用户输入101

My problem lays with printing out a specific student_id and the course that student is taking. Also using a for loop I couldn't find a way to find a duplicate. I can find an id that was last inputted by the user but when I enter an id from 2 previous inputs it passes my if else statements.

我的问题在于打印出一个特定的student_id和学生正在上课的课程。也使用for循环我找不到找到重复的方法。我可以找到用户最后输入的id,但是当我输入前2个输入的id时,它会传递我的if else语句。

Any help is appreciated.

任何帮助表示赞赏。

1 个解决方案

#1


1  

student info[10];

Here, info is array of 10 students so you will have to read it with index.

在这里,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]);
}

#1


1  

student info[10];

Here, info is array of 10 students so you will have to read it with index.

在这里,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]);
}