file read loop hangs after 1st pass

Its been a while since I've coded, so I apologise if this is a stupid mistake, but I'm having a problem reading in a file. it gets to the end of the 1st loop and hangs, I dont know if its because of the string array I'm using, but I cant figure it out.

the main program is this
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;

// Structure Definitions
const int NUM_OF_CITIES = 18;

// Function Prototypes
void fileRead(string[NUM_OF_CITIES], double[NUM_OF_CITIES], double[NUM_OF_CITIES], char[NUM_OF_CITIES]);

int main()
{
    string code[NUM_OF_CITIES], userCode;
    double lat[NUM_OF_CITIES], lon[NUM_OF_CITIES];
    int month, year;
    char zone[NUM_OF_CITIES];
    
    cout << "before file read call\n";
     // call the file input function
     fileRead(&code[NUM_OF_CITIES], &lat[NUM_OF_CITIES], &lon[NUM_OF_CITIES], &zone[NUM_OF_CITIES]);    

    cout <<"back in main\n";

       
    system("pause");
    
    return 0;
    
}

void fileRead(string code[NUM_OF_CITIES], double lat[NUM_OF_CITIES], double lon[NUM_OF_CITIES], char zone[NUM_OF_CITIES])
{
     
     cout << "file read\n";
// opens airport name and location file
    ifstream inFile;    
    inFile.open("cityinfo.txt");
    
    // checks to see if the file opened correctly    
    if (!inFile)
       cout << "Error opening file\n";
    else
    {
        int i = 0;
        // reads in all the element data
        while (!inFile.eof())
        {
           inFile >> code[i];
           cout << code[i];
           inFile >> lat[i];
           cout << lat[i];
           inFile >> lon[i];
           cout << lon[i];
           inFile >> zone[i];
           cout << zone[i];
           i++;
           cout << "end of loop " << i << endl;
        }
        // closes the file
        inFile.close();
        cout << "end of read\n";
    } 
}


and the cityinfo.txt is
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
APN
 45.07   83.57   E
ATL
 33.65   84.42   E
DCA
 38.85   77.03   E
DEN
 39.75  104.87   M
DFW
 32.90   97.03   C
DTW
 42.23   83.33   E
GRR
 42.88   85.52   E
JFK
 40.65   73.78   E
LAF
 40.42   86.93   E
LAN
 42.77   84.60   E
LAX
 33.93  118.40   P
MBS
 43.53   84.08   E
MIA
 25.82   80.28   E
MQT
 46.53   87.55   E
ORD
 41.98   87.90   C
SSM
 46.47   84.37   E
TVC
 44.73   85.58   E
YYZ
 43.67   79.63   E


Any help would be much appreciated
Change line 24 to fileRead(code, lat, lon, zone);. Your code doesn't work because you don't pass array to function. With &code[NUM_OF_CITIES] you are passing address of 18th element of the array.
Last edited on
thanks
Topic archived. No new replies allowed.