Nov 11, 2011 at 8:46am UTC
I am stuck at trying to loop information of a file that includes cities and their latitude and longitude into a structure array. My structure is called Place and has member variables are string city, int latitude, and int longitude. I'm trying to loop the file which is included in the command line, but I can't get it to work. Where am I doing it wrong? can someone give me a hint as to why it not working?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/******************************************************************************
Title : Distance.cpp
Author : Zain Saeed
Created on : October 17, 2011
Description :
Purpose :
Usage :
Build with : g++ -o Distance Distance.cpp Distance.h
Modifications :
******************************************************************************/
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
//#include "PlaceStruct.h"
using namespace std;
struct Place
{
string city;
double latitude;
double longitude;
};
int main(int argc, char * argv[])
{
ifstream fin (argv[1]);
if (fin.is_open())
{
while (fin.good())
{
Place location[4];
for (int i = 0; i < 4; i++)
{
fin >> location[i].city >> location[i].latitude >> location[i].longitude;
}
}
}
else
cout << "Unable to open file" ;
return 0;
}
This is my input file________________________________
Boston 42.357778 -71.061667
New_York 40.7833 -73.9667
Warsaw 52.23 21.010833
San_Francisco 37.7793, -122.4192
_____________________________________________________
Last edited on Nov 11, 2011 at 9:05am UTC
Nov 11, 2011 at 9:01pm UTC
You should declare your structs earlier than the loop. So move Place location[4];
up to line 31.
Also, instead of (fin.good()), I like to use (!fin.eof()) to loop while it is not the end of the infile. That's just me though.
Last edited on Nov 11, 2011 at 9:02pm UTC