首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++处理文本文件中的错误11分割

C++处理文本文件中的错误11分割
EN

Stack Overflow用户
提问于 2015-11-18 14:45:41
回答 1查看 455关注 0票数 1

我试图在Mac中用C++编写一个程序来处理具有以下数据的文本文件(table.txt):

汤姆50 60 70.5 曾傑瑞80.3 65 91 马克75.2 77 92.7 露西100 87.6 93

但是,在终端上运行它所得到的结果是,分段错误11:

汤姆50 60 70.5 曾傑瑞80.3 65 91 马克75.2 77 92.7 分割故障: 11

这是我的节目:

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct StudentList {
    string name;
    double scores[2];
};

int main() {

    ifstream marks;
    marks.open("table.txt");

    StudentList Student[50];

    int index = 0;

    string text;
    if (marks.fail()) {
        cout << "fail open" << endl;
    }

    while (marks >> text) {
        cout << text << " ";
        Student[index].name = text;
        marks >> Student[index].scores[0];
        cout << Student[index].scores[0] << " ";

        marks >> Student[index].scores[1];
        cout << Student[index].scores[1] << " ";

        marks >> Student[index].scores[2];
        cout << Student[index].scores[2] << " ";

        cout << endl;
        index++;
        cout << index << endl;
    }

    marks.close();

    return 0;
}

到底是什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-18 14:55:14

在C中,与大多数现代编程语言一样,数组索引从0开始,减速中的数字是大小,而不是最后一个索引。所以

代码语言:javascript
复制
double scores[2];

声明一个大小为2的数组,其索引为0和1。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33783050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档