我正在尝试运行一个代码,从文本文件中对泰坦尼克号幸存者的年龄进行排序。它编译得很好,但是当我选择选项 B(选项 A 尚未编写)时,程序终止时只是说“分段错误”。 这是文本文件的一个小示例,供参考。
29 1stClass TRUE
0.9 1stClass TRUE
2 1stClass FALSE
30 第一类错误
我已将错误隔离到处理文件的块(//实际处理),但我不确定到底是什么错误。
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>
#include <ctype.h>
void sortSurvivors();
void sortAgesLiving();
int main()
{
char options;
std::cout << "Titanic Data \nOptions \nA) Display count of people who lived and died... \nB) Display count of people who lived by age... \nPlease select option (A-B)...";
std::cin >> options;
switch (options)
{
case 'A':
sortSurvivors();
break;
case 'B':
sortAgesLiving();
break;
}
}
void sortSurvivors()
{
}
void sortAgesLiving()
{
std::ifstream inputFile;
std::string filename = "TitanicData.txt";
std::string age;
std::string classBoat;
std::string survival;
bool survived;
int eldest = 0;
//pre-sort processing
while (inputFile >> age >> classBoat >> survival)
{
int ageConv = stoi(age);
//G is for the ghetto fix I am pulling here, because I recieve an error when using "TRUE" as a string
char gchar = 'G';
survival += gchar;
if (survival == "TRUEG")
{
survived = true;
}
else
{
survived = false;
}
if (eldest < ageConv)
{
eldest = ageConv;
}
}
//initialize vector
std::vector<int> survivorVector;
for (int i = 0; i < eldest; i++)
{
survivorVector.push_back(0);
}
inputFile.open(filename);
//actual processing (ERROR HERE)
if (inputFile)
{
while (inputFile >> age >> classBoat >> survival)
{
int ageConv = stoi(age);
if (survived = true)
{
survivorVector[ageConv] = survivorVector[ageConv] + 1;
}
for (int j = 0; j <= eldest; j++)
{
std::cout << j << "\t" << survivorVector[j] << "\n";
}
}
// Close the file.
inputFile.close();
}
else
{
std::cout << "I don't know what broke, but uhhhhhhhhhh oops.";
}
}
像往常一样,我确定我忽略了一些愚蠢的东西。
答案 0 :(得分:0)
在 sortAgesLiving()
中,您在开始预排序处理之前忘记打开文件。因此,您的第一个阅读循环将根本无法阅读任何内容。因此 eldest
将保持为 0。
然后构造一个向量并填充它。但由于循环基于 eldest
,因此向量 survivorVector
将保持为空。
当您最终打开文件并读取它时,第一行将被视为幸存的行,因为您不小心将布尔值覆盖为 true(即 if (survived = true)
而不是 if (survived == true)
或只是 {{1} }. 然后您将尝试越界访问向量。
即使你纠正了这个错误,在第一个幸存者时你会再次出界。越界访问向量是 UB,许多可能的症状之一可能是分段错误。
其他建议(与您的问题无关):
if (survived)
年龄不明确。将其转换为 0.9
会使其为 0。这样可以吗,还是需要将其四舍五入?int
并直接读取而无需转换。然后,您可以在数学上将其转换为整数年龄,根据需要将其四舍五入或截断。如果您确定只有整数,则可以将变量设为 double
而不必担心。int
的额外行怎么办?如果读取的值为负怎么办?在使用值作为索引之前,最好始终检查它是否在可接受的范围内。它可以为您节省数小时的调试时间,并为您的客户节省一些噩梦。