The program I'm working on reads in a file line by line into and array of chars.
I am supposed to parse the line array and pull out state names capitals and population, put them into an array of structs.
The way it was explained to me is to parse the array in intervals character by charater but I realized that this will take 300+ lines of code.
What am I missing and is there a better way to do this.
Code:
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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct state_info
{
char state ;
char capital ;
long population;
};
//prototypes
int ReadStateInfo();
int main()
{
ReadStateInfo();
}
int ReadStateInfo()
{
state_info array[50];
ifstream infile("states.dat" );
if( !infile )
{
cerr << "Unable to open: \"" << endl;
return -1;
}
const int MAX_LINE = 128;
char line[MAX_LINE];
infile.getline(line, MAX_LINE);
for (int i = 0; i<49; i++)
{
cout << line[i] << endl;
//>> array[i].state >> array[i].capital >> array[i].population;
infile.getline(line, MAX_LINE);
}
infile.close();
int j = 0;
for (int n=5; n<15; n++)
{
if (isalpha(line[n]))
{
line[n] = array[n].state;
}
}
}
|
Source file format
Tennessee Nashville 5,700,037
New Mexico Santa Fe 1,823,821
Oregon Salem 3,428,543
Minnesota St. Paul 4,925,670
Wisconsin Madison 5,371,210
Pennsylvania Harrisburg 12,300,670