简体   繁体   中英

C Programming, Structs and Multidimensional arrays

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.

My struct.h look like this:

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

In main.c

student info[10];

In 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

Then Prompt the user for a course ID.

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

User inputs 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.

Any help is appreciated.

student info[10];

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

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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM