2014-03-04 6 views
0

누구든지 내 문제를 해결할 수 있는지 궁금합니다. 그래서 제가 연구하고있는 프로그램은 원하는 목적지가 아닌 다른 도시에서 멈추는 항공편과 비교하여 논스톱 항공편의 비행 거리를 계산하려고 시도합니다. 지금까지 나는 처음 두 개의 데이터 세트를 팝업으로 표시했지만 세 번째 데이터 세트를 얻으려고하면 두 번째 도시 세트를 얻습니다. 나는 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

죄송합니다. dataIn >> begLat 부분에 문제가 있습니다. 그 진술들을 많이 넣으려고했는데 한 세트의 데이터를 다른 것으로 대체하기 위해 getLine (데이터, 쓰레기)을 넣었습니다. void 함수를 사용하고 있는데이 함수는 void 함수가있는 첫 번째 프로그램입니다. – user3267868

답변

0

선택 사항 # 2와 (과) 동일한 내용을 읽는 것 같습니다. 기본적으로 동일한 데이터 세트를 읽는 중입니다. 선택 3에서 실제로 데이터를 읽기 전에 더 많은 데이터 행을 건너 뛸 필요가 있습니다. 따라서 귀하의 방법 (약간 순진 해 보임) 당 2 배의 getline (dataIn, trash)이 있어야합니다. 선택 2에서와 같이

+0

오케이. 미안하지만 그건 내가 아는 유일한 방법이야. 그 선을 계속해서 반복해서 쓸 필요가 없도록 더 나은 방법을 말해 줄 수 있습니까? – user3267868

+0

처음에는 for 또는 while 루프를 사용하여 불필요한 데이터를 건너 뛸 수있었습니다. 함수에 넣고 사용자의 선택에 따라 한 두 번 호출 할 수도 있습니다. 또 다른 방법은 배열을 항상 배열로 읽은 다음 선택에 따라 필요한 배열 항목에 액세스하는 것입니다. 어쩌면 ifstream도 당신이 더 효과적 이도록 도와 줄 수있는 함수를 가지고 있을지 모르지만, 실제로 그것에 대해 조사하지는 않았습니다. 행운을 빕니다. –

+0

어레이가 쉬워졌습니다. 말해 주셔서 감사합니다. 방금 프로그래밍을 시작했을 때 그다지 훌륭하지 않습니다. – user3267868

관련 문제