我试图在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
这是我的节目:
#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;
}到底是什么问题?
发布于 2015-11-18 14:55:14
在C中,与大多数现代编程语言一样,数组索引从0开始,减速中的数字是大小,而不是最后一个索引。所以
double scores[2];声明一个大小为2的数组,其索引为0和1。
https://stackoverflow.com/questions/33783050
复制相似问题