时零雨竹 2021-01-16 17:10 采纳率: 100%
浏览 7
已结题

为什么上面的程序可以运行,下面的就不可以?

//#include<iostream>
//using namespace std;
//#include<string>
//
//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];
//
//          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()
//{ 
//    struct teacher tarray[3];
//    int len=sizeof(tarray)/sizeof(tarray[0]);
//    allocatespace(tarray,len);
//    printinfo(tarray,len);
//    system("pause");
//    return 0;
//}


#include<iostream>
using namespace std;
#include<string>

struct student//学生
{
    string sname;
    int scroe;

};
struct teacher //老师
{
    string tname;
    struct student sarray[5];
};
void allocatespace(struct teacher tarray[], int len)  //赋值
{
    string nameseed = "ABDCE";
    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];

            tarray[i].sarray[j].scroe = 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; i++)
        {
            cout << "学生的姓名: " << tarray[i].sarray[j].sname
                << " 考试分数" << tarray[i].sarray[j].scroe << endl;
        }

    }
}
int main()
{
    struct teacher tarray[3];
    int len = sizeof(tarray) / sizeof(tarray[0]);
    allocatespace(tarray, len);
    printinfo(tarray, len);
    system("pasue");
    return 0;
}

 

  • 写回答

1条回答 默认 最新

  • 云中小筑 2021-01-16 18:04
    关注

    红框处应该是 j++。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频