/******************************************************************************************* 07P.cpp read data into an array of objects, then display them *******************************************************************************************/ #include <iostream> #include <string> #include <fstream> using namespace std; class soldier { public: string first; string last; string rank; int serial; }; //prototypes void readFrom(string file, soldier list[], const int size, int& count); void showList(soldier [], const int count); void main() { const int MaxSize = 25; soldier recruits[MaxSize]; int count = 0; string filename; cout << "What is the file to read: "; cin >> filename; readFrom(filename, recruits, MaxSize, count); showList(recruits, count); return; } void readFrom(string file, soldier list[], const int size, int& count) { ifstream fin(file.c_str()); for (int i = 0; i < size; i++) { if(fin >> list[i].rank >> list[i].first >> list[i].last >> list[i].serial) count++; else break; } return; } void showList(soldier list[], const int count) { for (int i = 0; i < count; i++) { cout << list[i].last << ", " list[i].first << " (" << list[i].serial << ") " << list[i].rank << endl; } cout << endl; return; }