/****************************************************************************************** ex1.cpp Working our way to dynamic criteria baseline example, working with state data ******************************************************************************************/ #include <iostream> #include <string> #include <fstream> using namespace std; // class definition class person { public: string first; string last; string state; }; // prototypes void readFile(string filename, person [], const int maximum, int& size); // MAIN! void main() { int count=0; const int maxsize=100; person census[maxsize]; string filename; cout << "Enter the file to read: "; cin >> filename; readFile(filename, census, maxsize, count); int countState = 0; for (int i = 0; i < count; i++) if (census[i].state == "NY") countState++; cout << countState << " records of " << count << " from New York." << endl; float percent = (float)countState/count; cout.precision(0); cout.setf(ios::fixed); cout << percent*100 << "% of records from New York." << endl; } //------- fill each person in the list from a file ------ void readFile(string filename, person people[], const int maximum, int& size) { ifstream fin(filename.c_str()); for (int i=0; i < maximum; i++) { if(fin >> people[i].first >> people[i].last >> people[i].state) size++; } }