首页 > 解决方案 > 使用结构的年度学生计划

问题描述

我的第一个问题是我无法制作 for 循环并将其实现到代码中并以某种方式完成我的程序。

我的第二个问题是编译器显示问题:memset(database,0,SIZE*sizeof(struct student));缺少类型说明符,默认为“int”

在程序结束时,我对memcpy(database.name,name,size_of_name);成员引用基类型“struct student [100]”有问题不是结构或联合

有我的结构和功能:

 #define SIZE 100

struct student {
char name[SIZE];
int votes;
};

struct student database[SIZE];
memset(database,0,SIZE*sizeof(struct student));
int size = 0;

int find_student(struct student* students,int size, const char* name){
    // for loop,which take all entries in database 
    // if it find same name, then reutrn his index 
    //otherwise reuturn -1;
    int i;
    for(i=0; i<SIZE;i++){
        // how to make that loop
    }
     return -1;
}

int compare(const void* p1, const void* p2){
    struct student* s1 = (struct student*)p1;
    struct student* s2 = (struct student*)p2;
    // s1->votes 
    // s1->name 
    return 0;
}

还有我已经做过的代码(对不起我的语法):

char line[SIZE];
memset(line,0,SIZE);
char* r = fgets(line,SIZE,stdin);
if (r == NULL){
    printf("End of input");
    return (-1);
}
char* end = NULL;
int value = strtol(line,&end,10);
if (value == 0){
    printf("Convertion was not sucessful");
    return (-1);
}

// helping array
char name[SIZE];
// set on zero
memset(name,0,SIZE);
// get begining of a name = one position after space
char* beginning_name = end + 1;
// Size of name is number of signs to end of string
// minus end of line
int size_of_name = strlen(beginning_name) - 1;
if (size_of_name > 0){
    // copy
    memcpy(name,beginning_name,size_of_name);
    // At the end is saved string with name 
    // without end of line and with zero at the end
}
else {
    // failed to read a name
    printf("Failed to read a name");
    return (-1);
}

int id = find_student(database,size,name);
if (id< 0){
    // copy it to last place in array 
    memcpy(database.name,name,size_of_name);
    // increase number of entries 
    size+=1;
}
else {
    // there I need add to votes,something like votes++;

}


}

抱歉格式化,但我是stackoverflow的新手。

标签: cstringpointersstructure

解决方案


推荐阅读