ISO C++ forbids assignment of Arrays.

I've been stuck on this for the past hour
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
/***************************************************************
Function: int buildArray( cityInfo cities[] )

Use: This function will read the file of data and fill an array with that data.

Arguments:  It takes as its argument the array of cityInfo structures.

Returns: It returns the number of valid cities that were placed in the array.
***************************************************************/
int buildArray( cityInfo cities[] )
{
    ifstream inFile;
    char cityName[25];
    int lowTemp;
    int highTemp;
    int numCities = 0;
    
    inFile.open( "binary_cities.txt", ios::binary );

    if ( inFile.fail() )
    {   
    cout << "The binary_cities.txt input file did not open";
    exit(-1);
    }

    inFile >> cityName;
    while (inFile)
    {
          inFile >> lowTemp;
          inFile >> highTemp;
          

          cities[numCities].cityName = cityName;          
          cities[numCities].lowTemp = lowTemp;
          cities[numCities].highTemp = highTemp;
          
          numCities++; 
          
          inFile >> cityName;
    }
    inFile.close();
    
    return numCities;
}


The compiler keeps returning ISO C++ forbids the assignment of Arrays and links the line that is bolded and underlined int he coding above. Any help on how to resolve the error?
Since a city name is likely a string, you could use strings. They are assignable:

just replace
char cityName[25];
with
string cityName;
You should use C standard function strcpy or strncpy instead of the assignment operator for the character arrays.
You didn't provide the definition of cityInfo.
I guessed it might look like this:
1
2
3
4
5
6
class cityInfo {
public:
    string cityName;
    int lowTemp;
    int highTemp;
};
The program compiles successfully with the above definition.

1
2
3
4
5
6
7
8
9
Alternatively, do it a bit like this:
class cityInfo {
public:
    char cityName[25];
    int lowTemp;
    int highTemp;
}; 

strcpy(cities[numCities].cityName, cityName);

If you use char arrays, then you need to become familiar with the common functions such as strclen, strcat, strcpy, strcmp and a few others.

See the reference page: http://www.cplusplus.com/reference/clibrary/cstring/
Topic archived. No new replies allowed.