我正在为需要根据一个人选择的“数据集”从输出文件中读取某些行的类制作程序。例如,如果一个人为所需的数据集输入“1”,我需要它使用数据文件的第 1 行到第 8 行(包括)。如果他们为所需的数据集输入“2”,我需要程序使用数据文件中的第 9 到 16 行(包括),如果是“3”,则使用第 17 到 24 行(包括)。这是我到目前为止的代码-
int main()
{
int latA, latB, latC, latD;
int longA, longB, longC, longD;
int AtoB, BtoC, CtoD, threeFlightTotal, nonStop;
int dataSet;
string cityA, cityB, cityC, cityD;
intro();
cout << "Which data set do you wish to use? 1, 2, or 3? ";
cin >> dataSet;
while(dataSet < 1 || dataSet > 3)
{
cout << "Sorry, that is not a valid choice. Please choose again." << endl;
cin >> dataSet;
}
ifstream dataIn;
dataIn.open("cities.txt");
if (dataIn.fail())
{
cout << "File does not exist " << endl;
system("pause");
exit(1);
}
else
{
cout << "File opened successfully" << endl;
}
dataIn.close();
system("pause");
return 0;
}
这是我的数据文件-
43.65 79.4
Toronto
40.75 74
New York
33.64 84.43
Atlanta
51.5 0
London
37.78 122.42
San Francisco
47.61 122.33
Seattle
44.88 93.22
Minneapolis
41.88 87.63
Chicago
21.19 157.5
Honolulu
45.31 122.41
Portland
42.2 83.03
Detroit
25.47 80.13
Miami
我该怎么做呢?我看过其他帖子,但我很难理解如何实施他们的解决方案。感谢您提前提供任何帮助。如果我没有提供足够的信息,请告诉我。