编写函数input和print,用结构体数组来输入打印5个学生的数据记录.结构体。

题目:
编写两个函数input和print,分别用来输入5个学生的数据记录和打印这5个学生的记录。对于每一个学生,其记录包含了学号、名字、3门课程的成绩共5项。用主函数分别调用input和print函数进行输入和输出。
要求使用结构体数组实现,结构体中包括了每个学生的5项记录。

输入:
共有5行,每行包含了一个学生的学号(整数)、名字(长度不超过19的无空格字符串)和3门课程的成绩(0至100之间的整数),用空格隔开。

输出:
与输入格式相同,每行输出一个学生的所有记录。
请注意行尾输出换行。

样例输入

101 AAA 80 81 82
102 BBB 83 84 85
103 CCC 86 87 88
104 DDD 89 90 91
105 EEE 92 93 94

样例输出:

101 AAA 80 81 82
102 BBB 83 84 85
103 CCC 86 87 88
104 DDD 89 90 91
105 EEE 92 93 94

解题 (完整):

#include
struct student {
    int num;
    char name[19];
    int scorea;
    int scoreb;
    int scorec;
};

student input(student s[5]){
	for (int i =0;i<5; i++){
		student *p;
    	p=s+i;       
        scanf("%d %s %d %d %d",&(*p).num,&(*p).name,&(*p).scorea,&(*p).scoreb,&(*p).scorec);      
    }         
}

student print(student s[5]){
	for (int i =0;i<5; i++){
		student *p;
    	p=s+i;       
        printf("%d %s %d %d %d\n",p->num,p->name,p->scorea,p->scoreb,p->scorec);      
    }  
}
int main(){
	student stu[5];
	input(stu);
	print(stu);    
	return 0;
}

纠正错误:
①**[Error] request for member ‘num’ in ‘* p’, which is of non-class type ‘char’**
编写函数input和print,用结构体数组来输入打印5个学生的数据记录.结构体。_第1张图片
int main()中定义的 s[5]*p 的类型都是 student (自身类型的变量
而在input()函数和print()函数中 s[5]*p的类型是char,故将改成student

而后出现
[Error] cannot convert ‘student’ to 'char’ for argument ‘1’ to 'void input(char)’**
编写函数input和print,用结构体数组来输入打印5个学生的数据记录.结构体。_第2张图片
input()函数和print()函数返回值类型是void 故改成student

而后出现
[Error] cannot convert ‘student’ to 'student’ for argument ‘1’ to 'student input(student)’**
编写函数input和print,用结构体数组来输入打印5个学生的数据记录.结构体。_第3张图片
百度……
编写函数input和print,用结构体数组来输入打印5个学生的数据记录.结构体。_第4张图片

编写函数input和print,用结构体数组来输入打印5个学生的数据记录.结构体。_第5张图片
原文链接:https://stackoverflow.com/questions/38104810/c-error-cannot-convert-students-to-students-struct-array-in-functio#

故我试了试:

int main(){
	student stu[5];
	input(stu);
	print(stu);    
	return 0;
}

可以,运行成功并提交正确。

你可能感兴趣的:(Codeup练习,Dev,C++错误)