我想知道是否有人可以幫我解決我的問題。因此,我正在研究的計劃試圖計算不停機航班的飛行距離,而不是停留在所需目的地以外的一個城市的航班。到目前爲止,我已經獲得了前兩個數據集,但是當我嘗試獲得第三個數據集時,我得到了第二組城市。我正在使用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);
}
很抱歉忘了縮進它。我在數據中遇到問題>>請求部分。我嘗試了一堆這些語句,並且還放入了getLine(data,trash)來替換一組數據。我正在使用void函數,這是我的第一個具有void函數的程序。 – user3267868