C++ 从单独的文本文件获取数据集

C++ 从单独的文本文件获取数据集,c++,void,C++,Void,我想知道是否有人能帮我解决这个问题。因此,我正在开发的程序试图计算直飞航班的飞行距离,并将其与在目的地以外的一个城市停留的航班进行比较。到目前为止,我已经得到了弹出的前两个数据集,但是当我试图得到第三个数据集时,我得到了第二组城市。我正在使用void函数,这是我第一个使用void函数的程序。我的日期来源代码为: #include <fstream> #include <iostream> #include <string> using namespace s

我想知道是否有人能帮我解决这个问题。因此,我正在开发的程序试图计算直飞航班的飞行距离,并将其与在目的地以外的一个城市停留的航班进行比较。到目前为止,我已经得到了弹出的前两个数据集,但是当我试图得到第三个数据集时,我得到了第二组城市。我正在使用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);
}

我想你读的选择3和你读的选择2是一样的。所以基本上你在读取相同的数据集。在选项3中,您需要在实际读取数据之前跳过更多数据行。因此,根据您的方法(这似乎有点幼稚),getlinedataIn、trash的数量应该是原来的两倍;与选项2相同。

抱歉,忘记正确缩进。我在dataIn>>begLat部分遇到问题。我试着把一堆这样的语句放进去,同时也放进getLine数据,用一组数据替换另一组数据。我正在使用void函数,这是我第一个使用void函数的程序。对不起,这是我现在知道的唯一方法。你能告诉我一个更好的方法,这样我就不必一遍又一遍地写这些行了吗?对于初学者,你可以使用For或while循环跳过不需要的数据。您还可以将其放入函数中,并根据用户的选择调用一次或两次。另一种选择是始终将集合读入数组,然后根据选择访问所需的数组项。也许ifstream还有一些功能可以帮助您提高效率,但我还没有真正研究过。祝你好运。数组更容易。谢谢你告诉我。我不太擅长编程,因为我刚刚开始。