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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了