For sake of simplicity i chose to use a type 'string' instead of 'char'. But i am supposed to lowercase the string that i read in from the input file. I did not know at the time i would not be able to use 'tolower()'. However i did find a way using 'transform'. But i cannot get it to work and cannot find example of using it with an array of stucts. Please help. And if at all possible i also have to capitalize the first letter(s) of each state so if you could point in in the right direction would be extremely grateful.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
struct ATdata
{
string state;
double miles;
int shelters;
};
int readData( ifstream& input, struct ATdata data[] );
size_t readData( ifstream& input, struct ATdata data[], size_t MAX_ENTRIES );
int main()
{
ifstream input;
char filename[256];
ATdata data[14];
int i;
cout << "Enter input file name: ";
cin >> filename;
input.open( filename );
if ( input.fail() )
{
cout << "Input file does not exist." << endl;
exit(1);
}
size_t linesRead = readData( input, data, sizeof ATdata data[], size_t MAX_ENTRIES );
input.close();
return(0);
}
size_t readData( ifstream& input, struct ATdata data[], size_t MAX_ENTRIES )
{
size_t i;
int j;
while ( i < MAX_ENTRIES && !input.eof() )
{
ATdata entry;
getline( input, entry.state );
transform( entry.state.begin(), entry.state.end(), entry.state.begin(), tolower() );
string nextLine;
if ( !getline( input, nextLine ) )
{
break;
}
istringstream iss( nextLine );
if ( !(iss >> entry.miles >> entry.shelters ) )
{
continue;
}
data[i++] = entry;
}
for ( j = 0; j < 14; j++ )
{
cout << data[j].state << data[j].miles << data[j].shelters << endl;
}
}
return i;