Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的分段错误11+;处理文本文件_C++_Segmentation Fault_Text Processing - Fatal编程技术网

C++ C+中的分段错误11+;处理文本文件

C++ C+中的分段错误11+;处理文本文件,c++,segmentation-fault,text-processing,C++,Segmentation Fault,Text Processing,我尝试在C++中编写一个程序来处理一个文本文件(table .txt),它有以下数据: 汤姆506070.5 杰里80.36591 马克75.27792.7 露西100 87.6 93 然而,我在终端上运行它得到的结果是,分段故障11: 汤姆506070.5 杰里80.36591 马克75.27792.7 分段错误:11 这是我的节目: #include <iostream> #include <fstream> #include <string> usin

我尝试在C++中编写一个程序来处理一个文本文件(table .txt),它有以下数据:

汤姆506070.5
杰里80.36591
马克75.27792.7
露西100 87.6 93

然而,我在终端上运行它得到的结果是,分段故障11:

汤姆506070.5
杰里80.36591
马克75.27792.7
分段错误:11

这是我的节目:

#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;
}
#包括
#包括
#包括
使用名称空间std;
结构学生列表{
字符串名;
双倍得分[2];
};
int main(){
流标记;
marks.open(“table.txt”);
学生名单学生[50];
int指数=0;
字符串文本;
if(marks.fail()){
cout(文本){
cout Student[索引]。分数[0];
cout Student[索引]。分数[1];
cout学生[指数]。分数[2];

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

double scores[2];

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

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

double scores[2];

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

您只为2个分数分配了空间:
双倍分数[2];
但尝试读取3:
分数>>学生[index]。分数[2];
访问数组超出范围是未定义的行为。您只为2个分数分配了空间:
双倍分数[2]
但试着阅读3:
标记>>学生[index]。分数[2];
访问数组超出范围是未定义的行为。@Michelle感谢的通常方式是向上投票/accept@Michelle通常的感谢方式是投票/接受