学校正在做毕设项目,每名teacher带领5个学生,总共有3名teacher,需求如下
设计学生和teacher的结构体,其中在teacher的结构体中,有teacher姓名和一个存放5名学生的数组作为成员
学生的成员有姓名、考试分数,创建数组存放3名teacher,通过函数给每个teacher及所带的学生赋值
最终打印出teacher数据以及teacher所带的学生数据。
#include<iostream>;
using namespace std;
#include<string>;
#include<ctime>;
struct Student {
string name;
int score;
};
struct Teacher {
string name;
struct Student sArray[5];
};
void printTeacher(struct Teacher tArray[], int len) {
for (int i = 0; i < len; i++) {
cout <<"老师的姓名为"<< tArray[i].name << endl;
for (int j = 0; j < 5; j++) {
cout << "\t学生的姓名为:" << tArray[i].sArray[j].name<<" 学生的成绩为:" << tArray[i].sArray[j].score << endl;
}
}
}
void allocateSpace(struct Teacher * tArray[], int* len) {
string nameseed = "ABCDE";
string tname = "教师";
string sname = "教师";
for (int i = 0; i < *len; i++) {
tArray[i]->name = tname + nameseed[i];
for (int j = 0; j < 5; j++) {
tArray[i]->sArray[j].name = sname + nameseed[j];
tArray[i]->sArray[j].score = rand() % 61 + 40;
}
}
}
int main() {
srand((unsigned int)time(NULL)); //随机数种子 头文件 #include <ctime>
struct Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
cout << tArray << endl;
allocateSpace(tArray, &len);
printTeacher(tArray, len);
system("pause");
return 0;
}
调用allocateSpace方法时它提示实参与形参不兼容
把指针去掉,使用值传递没有错误
在实参前加入&依然出错
应该如何修改代码在使用指针的情况下达到目的,为什么我这样写会出错
修改改成值传递结果如下:
#include<iostream>之后多了分号;
using namespace std;
#include<string>之后多了分号;
#include<ctime>之后多了分号;
tArray只是数组,不是指针
void allocateSpace(struct Teacher * tArray[], int* len) {
改成
void allocateSpace(struct Teacher tArray[], int* len) {
并且
tArray[i]->name
改成
tArray[i].name
tArray[i]->sArray[j].name
tArray[i]->sArray[j].score
改成
tArray[i].sArray[j].name
tArray[i].sArray[j].score
你题目的解答代码如下:
void allocateSpace(struct Teacher tArray[], int* len) {
string nameseed = "ABCDE";
string tname = "教师";
string sname = "教师";
for (int i = 0; i < *len; i++) {
tArray[i].name = tname + nameseed[i];
for (int j = 0; j < 5; j++) {
tArray[i].sArray[j].name = sname + nameseed[j];
tArray[i].sArray[j].score = rand() % 61 + 40;
}
}
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
#include<iostream>
using namespace std;
#include<string>
#include<ctime>
struct Student {
string name;
int score;
};
struct Teacher {
string name;
struct Student sArray[5];
};
void printTeacher(struct Teacher tArray[], int len) {
for (int i = 0; i < len; i++) {
cout <<"老师的姓名为"<< tArray[i].name << endl;
for (int j = 0; j < 5; j++) {
cout << "\t学生的姓名为:" << tArray[i].sArray[j].name<<" 学生的成绩为:" << tArray[i].sArray[j].score << endl;
}
}
}
void allocateSpace(struct Teacher tArray[], int* len) {
string nameseed = "ABCDE";
string tname = "教师";
string sname = "教师";
for (int i = 0; i < *len; i++) {
tArray[i].name = tname + nameseed[i];
for (int j = 0; j < 5; j++) {
tArray[i].sArray[j].name = sname + nameseed[j];
tArray[i].sArray[j].score = rand() % 61 + 40;
}
}
}
int main() {
srand((unsigned int)time(NULL)); //随机数种子 头文件 #include <ctime>
struct Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
cout << tArray << endl;
allocateSpace(tArray, &len);
printTeacher(tArray, len);
system("pause");
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!