小编Ngu*_*Tân的帖子

将cin字符串转换成node-> name时,为什么会出现分段错误(核心转储)?

我时出现分段错误(核心已转储)getline(cin, node->name)

我通过str在输入函数中声明一个字符串来修复,然后node->name = str。但是跑到线上cin >> node->year仍然击中了细分故障。

struct client
{
    int code;
    string name;
    int year;
    float maths, physics, chemistry;
    struct client *next;
};

struct client* input()
{
    struct client *node = (struct client *)malloc(sizeof(struct client));

    cout << "Code: ";
    cin >> node->code;

    cout << "Name: ";
    cin.ignore();
    getline(cin, node->name);

    cout << "Year: ";
    cin >> node->year;

    cout << "Maths, Physics, Chemistry: ";
    cin >> node->maths >> node->physics >> node->chemistry;

    node->next = …
Run Code Online (Sandbox Code Playgroud)

c++ string malloc segmentation-fault

4
推荐指数
1
解决办法
141
查看次数

标签 统计

c++ ×1

malloc ×1

segmentation-fault ×1

string ×1