#include #include #include #include //standards, instead of "using namespace std;" using std::cerr; using std::cout; using std::cin; using std::endl; using std::string; using std::setw; using std::ifstream; using std::ws; const int LessThan5 = 0; const int Age18to24 = 2; const int Age25to34 = 3; const int Age65to74 = 7; const int Age75to84 = 8; const int GtrThan85 = 9; class Population { private: //data members string state; int age[10]; public: //constructor method Population(); //accesor methods string get_state(void); void set_state(string); int get_age(int); void set_age(int, int); //other methods int total_pop(); }; //Constructor Population::Population() { state = ""; for (int i = 0; i < 10; i++) age[i] = 0; } //state string Population::get_state(void) { return state; } void Population::set_state(string x) { state = x; } //ages int Population::get_age(int i) { return age[i]; } void Population::set_age(int i, int a) { age[i] = a; } //total population function int Population::total_pop() { int total = 0; for (int i = 0; i < 10; i++) total += age[i]; return total; } //Functions int load_data(string , Population []); int least_under_5(Population [], int); //int most_under_5(Population [], int); //int least_18_to_34(Population [], int); //int most_18_to_34(Population [], int); //int least_over_64(Population [], int); //int most_over_64(Population [], int); //MAIN PROGRAM int main() { Population p[70]; string filename; cout << "File to open? "; cin >> filename; cout << load_data(filename, p) << " records read from " << filename << endl; /******NOT TO BE INCLUDED IN FINAL PROGRAM****** //get state and print for (int i=0;i<51;i++) { cout << p[i].get_state() << endl; //get ages and print for (int j=0;j<10;j++) { cout << setw(6) << p[i].get_age(j); } cout << endl; } ******NOT TO BE INCLUDED IN FINAL PROGRAM******/ //test cout << "Alabama total: " << p[0].total_pop() << endl; cout << "least is: " << least_under_5(p, 51); return 0; } //data_load function...[LessThan5] int load_data(string f, Population p[]) { string s; int ages, i = 0; ifstream file; file.open(f.c_str()); if (!file) { cerr << "Error, file not opening..." << endl; return 0; } else { //set state name getline(file, s); p[i].set_state(s); //set numbers for (int j = 0; j < 10; j++) { file >> ages >> ws; p[i].set_age(j, ages); } while (!file.eof()) { //increase count i++; //set state name getline(file, s); p[i].set_state(s); //set numbers for (int j = 0; j < 10; j++) { file >> ages >> ws; p[i].set_age(j, ages); } }//closing while file.close();//closing file }//closing else //element 0 needs to be counted for, //so we add one onto i for the total //number of elements used in the array. return i + 1; } int least_under_5(Population p[], int size) { int least = 0; for (int i = 1; i < size; i++) if (p[i].get_age(LessThan5) / p[i].total_pop() < p[least].get_age(LessThan5) / p[least].total_pop()) least = i; return least; }