/***************************************************************
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?
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.