我想知道是否有人可以帮我解决我的问题。所以我正在研究的程序试图计算非停靠航班的飞行距离,而不是在所需目的地以外的一个城市停靠的航班。到目前为止,我已经获得了前两个数据集,但是当我尝试获取第三个数据集时,我得到了第二组城市。我正在使用void函数,这是我的第一个带void函数的程序。我从中获取日期的部分的代码是:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void readFile(int choice, double &begLat, double &begLon, string &begCity,
double &midLat, double &midLon, string &midCity,
double &endLat, double &endLon, string &endCity);
void intro();
void askDataSet(int &selection);
int dist(double lat1, double lon1, double lat2, double lon2);
//--------------------------------------------------
int main()
{
intro();
double begLat, begLon, midLat, midLon, endLat, endLon;
string begCity, midCity, endCity;
int choice;
askDataSet(choice);
readFile(choice, begLat, begLon, begCity, midLat, midLon, midCity, endLat, endLon, endCity);
cout << "The First City at coordinates " << begLat << " and " << begLon << " is: " << begCity << endl;
cout << "The Second City at coordinates " << midLat << " and " << midLon << " is: " << midCity << endl;
cout << "The Third City at coordinates " << endLat << " and " << endLon << " is: " << endCity << endl;
int leg1, leg2, nonstop;
leg1 = dist(begLat, begLon, midLat, midLon);
leg2 = dist(midLat, midLon, endLat, endLon);
nonstop = dist(begLat, begLon, endLat, endLon);
cout << "The legs are " << leg1 << " and " << leg2 << endl;
cout << "Nonstop is " << nonstop << endl;
cout << "It is " << leg2 + leg1 - nonstop << " fewer miles for non-stop" << endl;
system("pause");
return 0;
}
void readFile(int choice, double &begLat, double &begLon, string &begCity,
double &midLat, double &midLon, string &midCity,
double &endLat, double &endLon, string &endCity)
{
ifstream dataIn;
dataIn.open("cities.txt");
if (dataIn.fail())
{
cout << "Error. File does not exist. " << endl;
exit(1);
}
if (choice == 1)
{
dataIn >> begLat;
dataIn >> begLon;
dataIn.get();
getline(dataIn, begCity);
dataIn >> midLat;
dataIn >> midLon;
dataIn.get();
getline(dataIn, midCity);
dataIn >> endLat;
dataIn >> endLon;
dataIn.get();
getline(dataIn, endCity);
}
string trash;
if (choice == 2)
{
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
dataIn >> begLat;
dataIn >> begLon;
dataIn.get();
getline(dataIn, begCity);
dataIn >> midLat;
dataIn >> midLon;
dataIn.get();
getline(dataIn, midCity);
dataIn >> endLat;
dataIn >> endLon;
dataIn.get();
getline(dataIn, endCity);
}
if (choice == 3)
{
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
getline(dataIn, trash);
dataIn >> begLat;
dataIn >> begLon;
dataIn.get();
getline(dataIn, begCity);
dataIn >> midLat;
dataIn >> midLon;
dataIn.get();
getline(dataIn, midCity);
dataIn >> endLat;
dataIn >> endLon;
dataIn.get();
getline(dataIn, endCity);
}
}
void intro()
{
cout << "In this lab we will try to figure out how much shorter it is to fly non-stop compared to one-stop." << endl;
cout << endl;
}
void askDataSet(int &selection)
{
cout << "Do you wish to use dataset 1, 2, or 3? ";
cin >> selection;
while (selection < 1 || selection > 3)
{
cout << "The only valid choices are 1-3. Please re-enter";
cin >> selection;
cout << endl;
}
}
int dist(double lat1, double lon1, double lat2, double lon2)
{
double diffLat, diffLon, hyp;
diffLat = (lat1 - lat2) * 69;
diffLon = (lon1 - lon2) * 53;
hyp = sqrt(diffLat * diffLat + diffLon * diffLon);
return static_cast<int>(hyp);
}
答案 0 :(得分:0)
我认为你正在阅读#3中的选择,就像阅读选择#2一样。所以基本上你正在阅读相同的数据集。在选择#3中,您需要在实际读取数据之前跳过更多数据行。所以根据你的方法(看起来有点天真),应该有两倍的getline(dataIn,trash);如选择#2。