// Scholarships.cpp // This program tutorial demonstrates arrays using vectors // It also demonstrates file I/O techniques with vectors // and making arrays of objects // The Datafile used in a CSV...

// Scholarships.cpp // This program tutorial demonstrates arrays using vectors // It also demonstrates file I/O techniques with vectors // and making arrays of objects // The Datafile used in a CSV (Comma Seperated Values) file // named Scholarships.csv // The structor of the data file is: // ID, Amount Awarded, Scholarship Type, Length of Scholarship, Date Starts, Last Name, First Name #include "scholarships.h" int main() { string sFileLine; // a string to read in each line of the file vector sParsedLine; // array to hold the parsed line from file vector scholars; // this object is initialized using default constructor // the default construtor // Open input and output files and test to make sure they openned correctly ifstream fin; ofstream fout; OpenFiles(fin, fout); while(!fin.eof()) //Read a line from the file and push onto end of scholars scholars.push_back(readFile(sFileLine, sParsedLine, fin)); int sArraySize = scholars.size(); // Get the size of the array for(int i = 0; i writeFile(scholars[i], fout); // Write a line to the output file // Write the summary report to the end of the output file by calling the // summary function and passing both the array of objects and the // file handle to the function createReportSummary(scholars, fout); // Close the original output report to reuse its file handler // and not overwrite the original output report fout.close(); fout.open("ScholarshipsReportByType.dat"); if (!fout) { cout <> exit(0); } // Ask the user for the specific report string sType; cout cin >> sType; cout <> for (int i = 0; i writeFile(scholars[i], fout, sType); // Write a line to the output file return 0; } void OpenFiles(ifstream &in, ofstream &out) // must be passed in by reference { in.open("Scholarships.csv"); if(!in) { cout <> exit(0); } out.open("ScholarshipsReport.dat"); if(!out) { cout <> exit(0); } } Scholarship readFile(string &sLine, // Pass in by reference to change string in main() vector &sParsed, // Pass in by reference to change array in main() ifstream &fin) // Also pass in the input file buffer by ref to read from { getline(fin,sLine); stringstream lineStream(sLine); // A special string class for pre-output formatting string field; sParsed.clear(); // Empty the Parsed Line for reuse while(getline(lineStream,field,',')) // While there are fields between the , { sParsed.push_back(field); // Push them onto the string array vector } // return a Scholarship object copy created with the initialization construction return Scholarship(sParsed[0], stoi(sParsed[1]), sParsed[2], sParsed[3], sParsed[4], sParsed[5], sParsed[6]); } void writeFile(Scholarship s, // Pass in by value- no need to change string in main() ofstream &fout) // Also pass in the output file buffer by ref to write to { createReportHeadings(fout); fout <> <> <> } void createReportHeadings(ofstream &fout) { fout <> <> } string addCommas(int num) // Adds commas to any number for formatted output to report files { string s = to_string(num); // Convert the number to string to hold the formatted number // Insert commas from right (at implied decimal point) to left int sSize = s.size(); // Index to last digit if (sSize > 3) for (int i = (sSize - 3); i > 0; i -= 3) s.insert(i, ","); return s; } void createReportSummary(vector sArray, // Pass by value (copy) the entire array ofstream &fout) // Pass the output file by reference { int total = 0; // Accumulator for total amount int sArraySize = sArray.size(); // Get the size of the array // Accumulators for type totals int baseTotal = 0, baskTotal = 0, dTotal = 0, fTotal = 0, gTotal = 0, softTotal = 0, swimTotal = 0, tTotal = 0, vTotal = 0; // Loop through the array to accumulate the total amount of all scholarships for (int i = 0; i { total += sArray[i].getAmount(); // Add the Amount of each scholarship to the total string sType = sArray[i].getType(); // Get the type of scholarship for this record // Add the Type's Amount to the appropriate accumulator if (sType == "Baseball") baseTotal += sArray[i].getAmount(); else if (sType == "Basketball") baskTotal += sArray[i].getAmount(); else if (sType == "Diving") dTotal += sArray[i].getAmount(); else if (sType == "Football") fTotal += sArray[i].getAmount(); else if (sType == "Golf") gTotal += sArray[i].getAmount(); else if (sType == "Softball") softTotal += sArray[i].getAmount(); else if (sType == "Swimming") swimTotal += sArray[i].getAmount(); else if (sType == "Track") tTotal += sArray[i].getAmount(); else if (sType == "Volleyball") vTotal += sArray[i].getAmount(); } // Write the summary report output line fout <> <> <> <> <> <> <> <> <> <> <> <> } // Overloaded function for report of specific type of scholarship void writeFile(Scholarship s, // Pass in by value no need to change string in main() ofstream &fout, // Also pass in the output file buffer by ref to write to string sType) // Pass in by value the type of scholarship { static int lineCount = 60; if (lineCount == 60) // Ready for next page { fout createReportHeadings(fout); lineCount = 0; } if (sType == s.getType()) { fout <> <> <> <> lineCount++; } }
Dec 15, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here