누구든지 내 문제를 해결할 수 있는지 궁금합니다. 그래서 제가 연구하고있는 프로그램은 원하는 목적지가 아닌 다른 도시에서 멈추는 항공편과 비교하여 논스톱 항공편의 비행 거리를 계산하려고 시도합니다. 지금까지 나는 처음 두 개의 데이터 세트를 팝업으로 표시했지만 세 번째 데이터 세트를 얻으려고하면 두 번째 도시 세트를 얻습니다. 나는 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);
}
죄송합니다. dataIn >> begLat 부분에 문제가 있습니다. 그 진술들을 많이 넣으려고했는데 한 세트의 데이터를 다른 것으로 대체하기 위해 getLine (데이터, 쓰레기)을 넣었습니다. void 함수를 사용하고 있는데이 함수는 void 함수가있는 첫 번째 프로그램입니다. – user3267868