//Lab 3 //Trevor Bouchillon #include #include // For the files!!!! #include // For manipulators & formatting options using std::cin; using std::cout; using std::endl; using std::setw; using std::ios; using std::string; using std::ifstream; using std::ofstream; const int EMPLOYEES = 20; const int MAX = 21; string OutFileName = "C:\\TEMP\\smallOut.txt"; string InFileName = "C:\\TEMP\\small.txt"; int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[]); void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[], int counter); void PrintTotalsAndSummary(ofstream& out, int totalRecords); int main() { char name[EMPLOYEES][MAX]; int age[EMPLOYEES]; int record_counter(0); ifstream inFile; // Notice how this automatically opens the file ofstream outFile(OutFileName); //changed output file to be seperate. inFile.open(InFileName); if (inFile.is_open()) { record_counter = ReadData(inFile, outFile, name, age); inFile.close(); if (outFile.is_open()) { WriteOutputFile(outFile, name, age, record_counter); PrintTotalsAndSummary(outFile, record_counter); outFile.close(); } else { cout << "Trouble Opening: " << OutFileName; cout << "\n\n\t\t ** About to EXIT NOW! ** "; } } else { cout << "Trouble Opening: " << InFileName; cout << "\n\n\t\t ** About to EXIT NOW! ** "; } return 0; } int ReadData(ifstream& inFile, ofstream& outFile, char name[][MAX], int age[]) { int counter = 0; inFile >> name[counter] >> age[counter]; // Priming Read while (!inFile.eof()) { cout << setiosflags(ios::left) << setw(25) << name[counter] << resetiosflags(ios::left) << setw(4) << age[counter] << endl; counter++; inFile >> name[counter] >> age[counter]; } return counter; } void WriteOutputFile(ofstream& outFile, char name[][MAX], int age[], int counter) { outFile << " Here is the Output File" << endl; for (int r = 0; r < counter; r++) //made r a less than rather than less than or equal to. { outFile << setiosflags(ios::left) << setw(25) << name[r] << setw(4) << resetiosflags(ios::left) << age[r] << endl; } } void PrintTotalsAndSummary(ofstream& outFile, int totalRecords) { // To screen cout << "\n\n\t** Total Records: " << totalRecords << " **\n" << "\t\t The End \n"; // To file outFile << "\n\n\t** Total Records: " << totalRecords << " **\n" << "\t\t The End \n"; }