2014-03-04 39 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

很抱歉忘了縮進它。我在數據中遇到問題>>請求部分。我嘗試了一堆這些語句,並且還放入了getLine(data,trash)來替換一組數據。我正在使用void函數,這是我的第一個具有void函數的程序。 – user3267868

回答

0

我覺得你正在閱讀選擇#3,就像你閱讀選擇#2一樣。所以基本上你正在閱讀相同的數據集。在選擇#3中,在實際讀取數據之前,您需要跳過更多數據行。因此,根據你的方法(這看起來有點天真),應該有兩倍的getline(dataIn,trash);如在選擇#2中那樣。

+0

哦,好的。對不起,但那是我現在唯一知道的方法。你能否告訴我一個更好的方法,以便我不必一遍又一遍地寫這些文字? – user3267868

+0

對於初學者,您可以使用for或while循環跳過不需要的數據。你也可以把它放在一個函數中,根據用戶的選擇調用它一次或兩次。另一種選擇是總是將這些集合讀入一個數組,然後根據選擇訪問所需的數組項。也許ifstream也有可以幫助你更有效的功能,但我沒有真正考慮過它。祝你好運。 –

+0

數組更容易。謝謝你告訴我。我剛開始編程時並不擅長編程。 – user3267868

相關問題