I found a way to read the data into my array structs but i cannot control how many it reads. It will read all 14 entries in the input file but after it has read the 14 it continues for a couple lines then stops. However, if i place the code to read data inside any loop whether while or for, it starts printing huge numbers instead of what is contained in the file. Does anyone know why this happens? Is there a way around it
Input file looks as follows( exclude the dot): There are 14 sets of data in the file. So, as shown below, set 1 would include georgia 76.4 10, then north carolina 95.5 20, and so on till 14 sets.
Data inside input file looks like this:
georgia
76.4 10
north carolina
95.5 20
and so on.
Need to print data to the screen like:
georgia 76.4 10
north carolina 95.5 20
etc.
The problem lies in my code were i attempt and horribly fail to read into the input file.
#include <iostream>
#include <fstream>
#include <cstdlib>
struct ATdata
{
string state;
double miles;
int shelters;
};
int readData( ifstream& input, struct ATdata data[] );
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);
}
readData( input, data );
cout << i << endl;
return(0);
}
int readData( ifstream& input, struct ATdata data[] )
{
int i, j;
getline( input, data[i].state, '\t' );
input.ignore( 14, '\n' );
input >> data[i].miles;
input.ignore( 1, ' ' );
input >> data[i].shelters;
for ( j = 0; j < 14; j++ )
{
cout << data[j].state << data[j].miles << data[j].shelters << endl;
}
}
I apologize for all the info trying to be as clear as can be as to not look more idiotic. Thank you have a nice day.