莫莫事 2021-11-03 18:03 采纳率: 100%
浏览 25
已结题

代码报错,初学者不知道怎么办,请讲解啊!!



```c++
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
//学生数组
struct Students
{
    //姓名
    string sName;
    //分数 
    int score;
};
//老师数组
struct Teacher
{
    //姓名
    string tName;
    //学生数组
    struct Students sArray[5];
};
//创建一个给老师和学生赋值的函数
void allocateSpace(struct Teacher tArray[3], int len)
{
    string nameseed = "ABCDE";
    //给老师开始赋值 
    for (int i = 0; i <= len; ++i)
    {
        tArray[i].tName = "Teacher_";
        tArray[i].tName += nameseed[i];
        //给老师带的学生赋值
        for (int j = 0; j <= 5; ++j) {
            tArray[i].sArray[i].sName = "Students_";
            tArray[i].sArray[i].sName += nameseed[i];
            tArray[i].sArray[i].score = 60;
        }
    }

}
//打印所有信息
void printinfo(struct Teacher tArray[], int len)
{
    for (int i = 0; i <= len; ++i)
    {
        cout << "老师的姓名:" << tArray[i].tName << endl;
        for (int j = 0; j <= 5; ++j)
        {
            cout << "学生的姓名:" << tArray[i].sArray[j].sName <<
                "考试分数:" << tArray[i].sArray[j].score << endl;
        }
    }
}
int main(void)
{
    //创建三名老师的数组
    struct Teacher tArray[3];
    //通过函数给三名老师赋值,并且给三名老师所带的五名学生的信息赋值 
    int len = sizeof(tArray) / sizeof(tArray[0]);
    allocateSpace(tArray, len);
    //打印
    printinfo(tArray, len);
    system("pause");
}

```

  • 写回答

2条回答 默认 最新

  • 关注

    你代码中的所有 i <= len 都改成 i <len
    所有的 j<=5都改成 j< 5
    代码修改如下:

    
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <ctime>
    using namespace std;
    //学生数组
    struct Students
    {
        //姓名
        string sName;
        //分数 
        int score;
    };
    //老师数组
    struct Teacher
    {
        //姓名
        string tName;
        //学生数组
        struct Students sArray[5];
    };
    //创建一个给老师和学生赋值的函数
    void allocateSpace(struct Teacher tArray[3], int len)
    {
        string nameseed = "ABCDE";
        //给老师开始赋值 
        for (int i = 0; i < len; ++i)
        {
            tArray[i].tName = "Teacher_";
            tArray[i].tName += nameseed[i];
            //给老师带的学生赋值
            for (int j = 0; j < 5; ++j) {
                tArray[i].sArray[j].sName = "Students_";
                tArray[i].sArray[j].sName += nameseed[i];
                tArray[i].sArray[j].score = 60;
            }
        }
    }
    //打印所有信息
    void printinfo(struct Teacher tArray[], int len)
    {
        for (int i = 0; i < len; ++i)
        {
            cout << "老师的姓名:" << tArray[i].tName << endl;
            for (int j = 0; j < 5; ++j)
            {
                cout << "学生的姓名:" << tArray[i].sArray[j].sName <<
                    "考试分数:" << tArray[i].sArray[j].score << endl;
            }
        }
    }
    int main(void)
    {
        //创建三名老师的数组
        struct Teacher tArray[3];
        //通过函数给三名老师赋值,并且给三名老师所带的五名学生的信息赋值 
        int len = sizeof(tArray) / sizeof(tArray[0]);
        allocateSpace(tArray, len);
        //打印
        printinfo(tArray, len);
        system("pause");
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月12日
  • 已采纳回答 11月4日
  • 创建了问题 11月3日

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持