w15861586189 2023-03-21 22:50 采纳率: 0%
浏览 22

关于#c++#的问题,请各位专家解答!



```c++

#include<iostream>  
#include<string>
#include<ctime>
using namespace std;
struct Student  //学生的结构体定义
{
    string sName;
    int score{};
};
struct Teacher  //老师的结构体定义
{
    string tName;
    struct Student sArray[5];
};

void allocateSpace(struct Teacher tArray[],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 = "Student_";
            tArray[i].sArray[j].sName += nameSeed[j];
            int random = rand() % 101;
            tArray[i].sArray[j].score = random;
        }
    }
}
void printInfo(struct Teacher tArray[i], int len)  //打印所有信息
{
    for (int i = 0; i < len; i++)
        cout << "老师姓名: " << tArray[i].tName << endl;
    for (int j = 0; j < 5; j++)
    {
        cout << "\t学生姓名: " << tArray[i].sArray[j].sName <<"考试分数: "<<tArray[i].sArray[j].score<<endl;
    }
}

int main()
{
    srand((unsigned int)time(NULL));
    struct Teacher tArray[3];//创建三名老师的数组

    //通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
    int len = sizeof(tArray) / sizeof(tArray[0]);
    allocateSpace(tArray, len);

    //打印所有老师及所带的学生信息
    printInfo(tArray,len);
    system("pause");
    return 0;
}

```
cout << "\t学生姓名: " << tArray[i].sArray[j].sName <<"考试分数: "<<tArray[i].sArray[j].score<<endl;
刚学习c++,请问各位专家解释一下为什么代码会在这一行的tArray[i]中报错未定义标识符i

  • 写回答

2条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-03-21 23:01
    关注

    printInfo方法有问题,修改如下:

    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 << "\t学生姓名: " << tArray[i].sArray[j].sName <<"考试分数: "<<tArray[i].sArray[j].score<<endl;
            }
        }
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月21日

悬赏问题

  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵