0

I am making a program for class that needs to read certain lines from an output file based on what "data set" a person chooses. For example, if a person inputs "1" for the desired data set, I need it to use lines 1 through 8 of the data file (inclusively). If they input "2" for the desired data set, I need the program to use lines 9 through 16 from the data file (inclusively), and if "3", then lines 17 through 24 (inclusively). Here is the code I have so far-

int main()
{
    int latA, latB, latC, latD;
    int longA, longB, longC, longD;
    int AtoB, BtoC, CtoD, threeFlightTotal, nonStop;
    int dataSet;
    string cityA, cityB, cityC, cityD;

    intro();

    cout << "Which data set do you wish to use? 1, 2, or 3?  ";
    cin >> dataSet;
    while(dataSet < 1 || dataSet > 3)
    {
        cout << "Sorry, that is not a valid choice. Please choose again." << endl;
        cin >> dataSet;
    }

    ifstream dataIn;
    dataIn.open("cities.txt");
    if (dataIn.fail())
    {
        cout << "File does not exist " << endl;
        system("pause");
        exit(1);
    }
    else
    {
        cout << "File opened successfully" << endl;
    }

    dataIn.close();

    system("pause");
    return 0;
}

Here is my data file-

43.65 79.4      
Toronto
40.75 74
New York
33.64 84.43
Atlanta
51.5 0
London
37.78 122.42
San Francisco
47.61 122.33
Seattle
44.88 93.22
Minneapolis
41.88 87.63
Chicago
21.19 157.5
Honolulu
45.31 122.41
Portland
42.2 83.03
Detroit
25.47 80.13
Miami

How would I go about doing this? I've looked at other posts but I am having a hard time understanding how to implement their solutions to mine. Thank you for any help in advance. If I'm not giving enough information let me know.

Sosa
  • 37
  • 6
  • You have a lot of variables that are declared, unitialized, and currently unused. Those `int` variables could have any kind of value and not initializing those can make for problems if you try and use one later without setting a value. A good practice is to declare the variables at the place where they are needed and give them a value at that time. – crashmstr Oct 09 '15 at 12:35

1 Answers1

0

You can simply skip the unneeded lines:

//here you calculate the amount of lines to skip.
//if dataSet=1 --> linesToSkip=0, if dataSet=2 --> linesToSkip=8...
int linesToSkipt = (dataSet-1) * 8;

//getline Needs a string to load the Content.
//So we don't use the data but wee Need to store it somewhere
std::string helper;

//We use a for Loop to skip the desired amount of lines
for(int i = 0; i < linesToSkip; ++i)
    std::getline(dataIn, helper);     //Skip the unneeded lines

If you knew the exact length of one line you could simply seek to desired Position. But from your example data set it seems like you don't. So you Need to read the file line by line until you reach the desired Position.

Thomas Sparber
  • 2,827
  • 2
  • 18
  • 34
  • Thank you for your response. May I ask you to explain exactly what this code does? Just so I can understand how exactly it works. – Sosa Oct 09 '15 at 12:04
  • So this code will inclusively use lines 1 through 8 if the person picks data set 1, and lines 9 through 17 if they pick data set 2, and so on, correct? – Sosa Oct 09 '15 at 12:23
  • It uses the first 9 lines for data set 1, the second 9 lines for data set 2, the third 9 lines for data set 3 and so on... You are not really consistene: 1 through 8 = 8 lines, but 9 trough 17 = 9 lines – Thomas Sparber Oct 09 '15 at 12:26
  • I apologize, each data set needs to read 8 lines, so it would actually be data set 1= lines 1-8, data set 2 = lines 9-16, and data set 3= lines 17-24. Mistake with math on my part. – Sosa Oct 09 '15 at 12:28
  • Ah ok i understand... So you just Need to Change the first line to `int linesToSkipt = (dataSet-1) * 8;` I already updated my answer. – Thomas Sparber Oct 09 '15 at 12:29
  • Thank you very much for your help. Sorry about all of that. – Sosa Oct 09 '15 at 12:33