简体   繁体   中英

How to access structure array from other function and assign value

My problem is that if i pass the global variable structure array and integer value counter from the main function to the menu2 function, the contents of the structure array need to be modified, but nothing has changed. For example, when i call menu2 function in main function and write the contents, nothing happen when i call Menu 1, 3, and 4 after call menu2. I've already try changed all the "List" on Menu 2 to "list". Can you help me just once?

#include<stdio.h>
#include<string.h>

#define MAX_STUDENT 100
#define YEAR_STUDENT 7
#define MAX_NAMELEN 20


char convertScore(int score)
{
    if (score > 89)
        return 'A';
    else if (score > 79)
        return 'B';
    else if (score > 69)
        return 'C';
    else if (score > 59)
        return 'D';
    else
        return 'F';
}

typedef struct student {
    char name[MAX_STUDENT+1];
    int scoreSecurity;
    char gradeSecurity;
    int scoreAI;
    char gradeAI;
    int scoreAlgorithm;
    char gradeAlgorithm;
    int scoreDataStructure;
    char gradeDataStructure;
    int scoreOS;
    char gradeOS;
    int gradeF;
    char year[YEAR_STUDENT];
}Student;

Student List[MAX_STUDENT];
int Counter = 0;

void menu1(Student* list, int cnt)
{
    int i;
    printf("Name\tAI\tSecurity\tDataStructure\tOS\tAlgorithm\tYear\n");
    for (i = 0; i < cnt; i++) {
        printf("%s\t%c\t%c\t%c\t%c\t%c\t%s\n", List[i].name, List[i].gradeAI,
            List[i].gradeSecurity,List[i].gradeDataStructure, List[i].gradeOS, List[i].gradeAlgorithm,
            List[i].year);
    }
    printf("\n");
}

void menu2(Student* list, int cnt)
{
    char sn[21];
    printf("Student name: ");
    scanf("%s", sn);
    strcpy(List[cnt].name, sn);
    printf("Student score AI(0~100): ");
    scanf("%d", &List[cnt].scoreAI);
    List[cnt].gradeAI = convertScore(List[cnt].scoreAI);
    printf("Student score Security(0~100): ");
    scanf("%d", &List[cnt].scoreSecurity);
    List[cnt].gradeSecurity = convertScore(List[cnt].scoreSecurity);
    printf("Student score Datastructure(0~100): ");
    scanf("%d", &List[cnt].scoreDataStructure);
    List[cnt].gradeDataStructure = convertScore(List[cnt].scoreDataStructure);
    printf("Student score OS(0~100): ");
    scanf("%d", &List[cnt].scoreOS);
    List[cnt].gradeOS = convertScore(List[cnt].scoreOS);
    printf("Student score Algorithm(0~100): ");
    scanf("%d", &List[cnt].scoreAlgorithm);
    List[cnt].gradeAlgorithm = convertScore(List[cnt].scoreAlgorithm);
    printf("Student year(Junior/Senior): ");
    scanf("%s", &List[cnt].year);
    printf("\n");
}

void menu3(Student* list, int cnt)
{
    int junior=0, senior=0, i;
    for (i = 0; i < cnt; i++) {
        if (List[i].year == "Junior")
            junior++;
        else
            senior++;
    }
    printf("Junior: %d\nSenior: %d\n", junior, senior);
}
void menu4(Student* list, int cnt)
{
    int i;
    int fail = 0;
    int suck = 0;
    int yeah = 0;
    for (i = 0; i < cnt; i++) {
        if (List[i].gradeAI == 'F')
            fail++;
        else if (List[i].gradeAlgorithm == 'F')
            fail++;
        else if (List[i].gradeDataStructure == 'F')
            fail++;
        else if (List[i].gradeOS == 'F')
            fail++;
        else if (List[i].gradeSecurity == 'F')
            fail++;

        if (fail > 2)
            suck++;
        else
            yeah++;
        fail = 0;
    }
    printf("Passed: %d\nFailed: %d\n", yeah, suck);
}

First, in menu2 , scanf("%s", &List[cnt].year); should be scanf("%s", List[cnt].year); . Second, I think you should consider how your Counter work. For example, after you call menu2 , and then menu1 , nothing can be printed. The reason is that in menu1 , the for loop you cannot go in to execute anything. At that time, your Counter is 0, that is, cnt in menu1 is 0.

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