So I have question regarding a program I need to write.

I need to read a text file which consists of a location and the coordinates of it's longitude and latitude. The program should be able to read any amount of spaces and tabs in between the tokens. So it could be something like this:

location latitude longitude
location [tab] latitude [tab] longitude
etc

My problem is that I'm a bit fuzzy on how to extract the location into an class array. I know I'm supposed to use the getline function but I'm having trouble figuring out how to make it place it into the class array and skip over the tabs and spaces. I have code... but it's still in the very early stages. Problem is I can't really continue unless I manage to get this part. I have a feeling that I will be able to write the whole thing once I manage to get this part down.

The main.cpp

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

#include "greatCircle.h"
using namespace std;

int main (int argc, char*argv[])
{
    ifstream fin;
    ofstream fout;
    Place destination;
    int i=0;
    
    //Open file specified on command line.
    fin.open (argv[1]);
    //If it cannot open the file exit the program.
    if (fin.fail())
    {
       cout <<"Could not open the input file. \n";
       exit(1);
    }
    //Open file specified on command line.
    fout.open (argv[2]);
    //If it cannot open the file exit the program.
    if (fout.fail())
    {
       cout <<"Could not open the output file. \n";
       exit(1);
    }
    //Initialized to " " as a way of checking which indexes is being 
    //used
    destination.location [array_size] = " ";
    /*Initialized to 10000 because no longitude or latitiude can reach
    that high. It is used the same way as above, to check which 
    indexes is used*/
    destination.longitude [array_size] = {10000.0};
    destination.latitude [array_size] = {10000.0};
    
    while (! fin.eof())
    {  
       getline (fin, destination.location[i],' '); 
       getline (fin, destination.latitude[i],' ');
       getline (fin, destination.longitude[i],'\n');  
       i++;
    }   
    fin.close();
    fout.close();
    return 0;
}

The header file.

#include <iostream>
#include <string>

using namespace std;

const int array_size = 30;
class Place
{
      public:
             void sort ();
             int min_number;
             void swap();
             double great_circle();
             void convertdg_rd();
             string location [array_size];
             double longitude [array_size];
             double latitude [array_size]; 
};

I know that this most likely have tons and tons of errors on it already but it's my first time using classes, seperate compliation and I/O streams. At the moment I'm getting "expected primary-expression before '{' token" and "no matching function for call to `getline(std::ifstream&, double&, char)'" errors. Thanks for the help.

1. get rid of the curly braces on lines 38 and 39.
2. your 2nd and 3rd calls to getline() won't work, because you're trying to pass a double in the second parameter, and getline() expects a streamsize (int) variable here. But I don't think you want a getline() here anyway. Probably you want more like a:

fin >> destination.latitude[i];

There may be other errors, but these jumped out at me first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.