/******************************************************************************************* volunteers.cpp read data into an array of objects, display them and do some calculations on them *******************************************************************************************/ #include <iostream> #include <string> #include <fstream> using namespace std; #include "safearray.h" //prototypes void readFrom(string file, safearray& list); void showList(const safearray&); int countPrivates(const safearray&); void main() { safearray volunteers; string filename; cout << "What is the file to read: "; cin >> filename; readFrom(filename, volunteers); showList(volunteers); cout << "-----------------------" << endl << "Privates: " << countPrivates(volunteers) << endl; } int countPrivates(const safearray& list) { int sum = 0; for (int i = 0; i < list.count(); i++) if (list[i].rank == "Private") sum++; return sum; } void showList(const safearray& list) { for (int i = 0; i < list.count(); i++) { cout << list[i].last << ", " << list[i].first << " (" << list[i].rank << ")" << endl; } } void readFrom(string file, safearray& list) { ifstream fin(file.c_str()); for (int i = 0; i < list.maxsize; i++) { string rank; if(fin >> rank) { list[i].rank = rank; fin >> list[i].first >> list[i].last; } else break; } }