weixin_40050831 2019-09-30 15:56 采纳率: 0%
浏览 320

c++结构体嵌套输入异常

如题:
要实现一个线性表的结构,但是在一开始初始化赋值时出现了问题。

```# include

include

include

using namespace std;
#define INIT_SIZE 1000
#define INCREMENT 1000

typedef struct {
string num;
string name;
float score;
}student , *student_l;
typedef struct {
student_l Student_List;
int length;
int listsize;
}List;

int Initial_List(List &L, int n =0 ) //初始化表格,分配空间
{
L.Student_List = (student_l)malloc(INIT_SIZE * sizeof(student));
if (!L.Student_List) return -1;
string student_name;
string student_num;
float student_score = 0;
/*for (int i = 0; i < n; i++)
{
cin >> student_num >> student_name >> student_score;

    L.Student_List[i].name = student_name;
    L.Student_List[i].num = student_num;
    L.Student_List[i].score = student_score;
}*/
L.length = n;
L.listsize = INIT_SIZE;
return 0;

}

int main()
{
int student_number = 0;
List student_List;
cin >> student_number;
Initial_List(student_List, student_number);

return 0;
system("pause");

}

  • 写回答

2条回答 默认 最新

  • 空白如空 2019-09-30 16:44
    关注

    我帮你运行了下,INIT_SIZE值设置小一点就可以正常运行,可以的话,请采纳一下

    #include<iostream>
    #include <stdlib.h>
    using namespace std;
    #define INIT_SIZE 100
    #define INCREMENT 1000
    
    typedef struct {
    string num;
    string name;
    float score;
    }student , *student_l;
    typedef struct {
    student_l Student_List;
    int length;
    int listsize;
    }List;
    
    int Initial_List(List &L, int n =0 ) //初始化表格,分配空间
    {
        //L.Student_List = (student_l)malloc(INIT_SIZE * sizeof(student));
        //L.Student_List = new student[INIT_SIZE];
        L.Student_List = (student_l)malloc(INIT_SIZE * sizeof(student));
        if (!L.Student_List) return -1;
        string student_name;
        string student_num;
        float student_score = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> student_num >> student_name >> student_score;
            L.Student_List[i].name = student_name;
            L.Student_List[i].num = student_num;
            L.Student_List[i].score = student_score;
        }
        L.length = n;
        L.listsize = INIT_SIZE;
        return 0;
    }
    int main()
    {
    int student_number = 0;
    List student_List;
    cin >> student_number;
    Initial_List(student_List, student_number);
    return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置