Question
I have a C++ program that I'm writing. The program so far takes in a .csv file removes duplicates from the file and outputs the
I have a C++ program that I'm writing. The program so far takes in a .csv file removes duplicates from the file and outputs the contents into a new .csv file. I now need to change the program to also sort the data by the department field first and the item code field second in ascending order. After writing data to the output file, I need to outpi\ut a tabular style report to standard output (do not output the report to a file). Include in the report the appropriate headers for each column. Please assume that the report will print in portrait mode on a standard US Letter size paper. For the report center the titles in each header. Left justify the data in each column with the exception of the floating point columns which are right justified. Floating point numbers should be displayed to two decimal places. The data should fit on in the portrait format. The total column is the result of the quantity times the cost.
The program should not be designed for just the provided data file, it should be able to run other files of the same format.
The database file is called database.csv and the contents are:
1,Outdoors,13107-070,257,2.71 2,Computers,66389-0001,948,94.43 3,Movies,53808-0776,278,65.75 4,Baby,41250-416,310,99.52 5,Toys,64058-413,783,45.65 6,Electronics,56062-422,219,20.95 7,Games,37808-453,478,41.23 8,Books,0268-1154,976,65.17 9,Toys,17089-075,484,67.69 10,Automotive,68788-9852,501,71.57 11,Electronics,37000-265,189,27.97 12,Jewelry,0527-1414,68,32.81 13,Toys,64092-113,450,55.23 14,Clothing,14783-015,975,97.03 15,Baby,68084-045,809,55.3 16,Music,42507-158,362,56.92 17,Games,36987-1476,203,36.95 18,Kids,50844-428,606,20.93 19,Baby,0781-5234,430,16.24 20,Automotive,0378-1049,82,81.32 21,Music,64679-701,487,28.77 22,Outdoors,63739-141,195,83.23 23,Books,0268-1154,976,65.17 24,Baby,76519-1003,430,16.24 25,Games,65342-1393,496,69.07 26,Electronics,0409-9630,797,92.55 27,Jewelery,33261-028,500,66.3 28,Computers,66336-058,375,84.16 29,Outdoors,51672-1330,128,68.85 30,Movies,53808-0776,278,65.75 31,Beauty,48951-8130,725,83.42 32,Baby,59779-224,848,82 33,Industrial,55711-070,753,46.48 34,Industrial,76446-002,272,89.03 35,Sports,68151-2870,185,2.86 36,Toys,0245-0709,783,45.65 37,Games,49999-963,523,93.65 38,Beauty,52125-508,500,2.38 39,Toys,54092-381,783,45.65 40,Beauty,55154-6649,666,79.52 41,Jewelry,57664-327,46,10.28 42,Grocery,49738-453,317,29 43,Grocery,68382-792,266,48.63 44,Outdoors,0268-6731,335,70.04 45,Beauty,68788-9494,12,16.81 46,Beauty,52125-508,500,2.38 47,Outdoors,33261-144,809,2.95 48,Computers,0641-6040,104,88.13 49,Automotive,0781-3059,577,95.24 50,Kids,60429-038,459,29.68 51,Sports,10578-024,185,2.86 52,Toys,64092-113,450,55.23 53,Jewelry,55111-586,297,53.61 54,Automotive,30142-289,282,33
Here is the program so far:
Record.h ------- #ifndef RECORD_H_ #define RECORD_H_ #include
Record.cpp ------- #include
using namespace std;
Record::Record(string s){ int index1 = s.find(","); int index2 = s.find(",", index1+1);
department = s.substr(index1+1, index2-index1-1);
index1 = index2 + 1; index2 = s.find(",", index1+1); item_code = s.substr(index1, index2-index1);
index1 = index2 + 1; index2 = s.find(",", index1+1); quantity = atoi(s.substr(index1, index2-index1).c_str());
index1 = index2 + 1; cost = atof(s.substr(index1).c_str());
}
Record::~Record(){
}
//Overloaded == and < operators bool operator ==(const Record &a, const Record &b) { if(a.department == b.department && a.item_code == b.item_code && a.quantity == b.quantity && a.cost == b.cost) return true; else return false; } //Overload << operator std::ostream& operator <<(std::ostream& out, const Record& r) {
out << r.department << "," << r.item_code << "," << r.quantity << "," << r.cost; return out; }
main.cpp =-------- #include
int main(){ vector
for(int i = 0; i < records.size();) { bool dupe = false; for(int j = 0; j < i; j++) { if(records[j] == records[i]) { dupe = true; break;
} }
if(!dupe) i++; else records.erase(records.begin() + i);
}
//compute output filename int index = filename.find_last_of("/"); string outfilename;
if(index == string::npos) outfilename = "new_" + filename; else outfilename = filename.substr(0, index) + "/new_" +filename.substr(index+1);
ofstream outfile(outfilename.c_str());
if(outfile.fail()) { cout << "Could not open output file " << outfilename << endl; return 1; }
for(int i =0 ;i < records.size(); i++) { outfile << (i+1) << "," << records[i] << endl; } outfile.close();
cout << "output written to file " << outfilename << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started