Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a C++ program that I\'m writing. The program so far takes in a .csv file

ID: 3712491 • Letter: I

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 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 outpiut 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 <iostream>
//record class
class Record{
public:
//Constructor
Record(std::string s);
//De-constructor
virtual ~Record();
//Overloaded == and < operators
friend bool operator ==(const Record &a, const Record &b);
//friend bool operator <(const Record &a, const Record &b);
//Overload << operator
friend std::ostream& operator <<(std::ostream&, const Record&);

private:
std::string department;
std::string item_code;
int quantity;
double cost;
};
#endif

Record.cpp
-------
#include <string>
#include <cctype>
#include <cstdlib>
#include "Record.h"

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 <iostream>
#include <fstream>
#include <string.h>
#include <vector>
#include <algorithm>
#include "Record.h"
using namespace std;

int main(){
vector<Record> records;
string filename;
cout << "Please enter the name of your file with the file path: " << endl;
cin >> filename;
ifstream ifs(filename.c_str());
if(!ifs){
cerr<< "can't open file " << filename << endl;
return 1;
}
string line;
while(getline(ifs, line)){
records.push_back(Record(line));
}
ifs.close();

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;
}

Explanation / Answer

Please find the code below. Output alignment may fail here. Try it on terminal in linux and it will display properly.

Record.h

#ifndef RECORD_H_
#define RECORD_H_
#include <iostream>
//record class
class Record{
public:
//Constructor
Record(std::string s);
//De-constructor
virtual ~Record();
//Overloaded == and < operators
friend bool operator ==(const Record &a, const Record &b);
//friend bool operator <(const Record &a, const Record &b);
//Overload << operator
friend std::ostream& operator <<(std::ostream&, const Record&);

//Added getters for private members as needed to print the data.
std::string getDepartment();
std::string getItemCode();
double getCost();
int getQuantity();

private:
std::string department;
std::string item_code;
int quantity;
double cost;
};
#endif

Record.cpp

#include <string>
#include <cctype>
#include <cstdlib>
#include "Record.h"

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;
}

std::string Record::getDepartment() {
    return department;
}

std::string Record::getItemCode() {
    return item_code;
}

double Record::getCost() {
    return cost;
}

int Record::getQuantity() {
    return quantity;
}

Main.cpp

#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
#include <algorithm>
#include <iomanip>
#include "Record.h"
using namespace std;

// comparator function for storing the vector according to the given criteria.
// Sort priority to department and then to item code.
bool recComp(Record a, Record b) {
    // check if department are equal.
    if(a.getDepartment()!=b.getDepartment())
        return a.getDepartment()< b.getDepartment();
    // if department not equal return the difference between itemcode.
    return a.getItemCode()< b.getItemCode();
}

// Have not commented in your code only commented the changes done.
int main(){
    vector<Record> records;
    string filename;
    cout << "Please enter the name of your file with the file path: " << endl;
    cin >> filename;
    ifstream ifs(filename.c_str());
    if(!ifs){
        cerr<< "can't open file " << filename << endl;
        return 1;
    }
    string line;
    while(getline(ifs, line)){
        records.push_back(Record(line));
    }
    ifs.close();
  
    // Added line for storing the records sorted according to their department
    // name and then their item code.
    sort(records.begin(), records.end(), recComp);

    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();

    // The above code is same as yours.

    cout << "output written to file " << outfilename << endl;

    // Centrally aligned the headers.

    cout<<"+------+-----------------------+-----------------+--------------+----------+ ";
    cout<<"| SRNO |      DEPARTMENT       |    ITEM CODE    |   QUANTITY   +   COST   | ";
    cout<<"+------+-----------------------+-----------------+--------------+----------+ ";
    for(int i = 0; i<records.size(); i++)
    {
        // left align serial number
        cout<<"| "<<std::left<<std::setw(4)<<(i+1)<<" |";
        // left align department with width 21
        cout<<" "<<std::left<<std::setw(21)<<records[i].getDepartment()<<" |";
        // left align itemcode with width 15
        cout<<" "<<std::left<<std::setw(15)<<records[i].getItemCode()<<" |";
        // right align quantity with width 12
        cout<<" "<<std::right<<std::setw(12)<<records[i].getQuantity()<<" |";
        // right align cost with width 8
        cout<<" "<<std::right<<std::setw(8)<<records[i].getCost()<<" |";
        cout<<endl;
    }
    cout<<"+------+-----------------------+-----------------+--------------+----------+ ";


    return 0;
}

Output

Please enter the name of your file with the file path:
database.csv
output written to file new_database.csv
+------+-----------------------+-----------------+--------------+----------+
| SRNO |      DEPARTMENT       |    ITEM CODE    |   QUANTITY   +   COST   |
+------+-----------------------+-----------------+--------------+----------+
| 1    | Automotive            | 0378-1049       |           82 |    81.32 |
| 2    | Automotive            | 0781-3059       |          577 |    95.24 |
| 3    | Automotive            | 30142-289       |          282 |       33 |
| 4    | Automotive            | 68788-9852      |          501 |    71.57 |
| 5    | Baby                  | 0781-5234       |          430 |    16.24 |
| 6    | Baby                  | 41250-416       |          310 |    99.52 |
| 7    | Baby                  | 59779-224       |          848 |       82 |
| 8    | Baby                  | 68084-045       |          809 |     55.3 |
| 9    | Baby                  | 76519-1003      |          430 |    16.24 |
| 10   | Beauty                | 48951-8130      |          725 |    83.42 |
| 11   | Beauty                | 52125-508       |          500 |     2.38 |
| 12   | Beauty                | 55154-6649      |          666 |    79.52 |
| 13   | Beauty                | 68788-9494      |           12 |    16.81 |
| 14   | Books                 | 0268-1154       |          976 |    65.17 |
| 15   | Clothing              | 14783-015       |          975 |    97.03 |
| 16   | Computers             | 0641-6040       |          104 |    88.13 |
| 17   | Computers             | 66336-058       |          375 |    84.16 |
| 18   | Computers             | 66389-0001      |          948 |    94.43 |
| 19   | Electronics           | 0409-9630       |          797 |    92.55 |
| 20   | Electronics           | 37000-265       |          189 |    27.97 |
| 21   | Electronics           | 56062-422       |          219 |    20.95 |
| 22   | Games                 | 36987-1476      |          203 |    36.95 |
| 23   | Games                 | 37808-453       |          478 |    41.23 |
| 24   | Games                 | 49999-963       |          523 |    93.65 |
| 25   | Games                 | 65342-1393      |          496 |    69.07 |
| 26   | Grocery               | 49738-453       |          317 |       29 |
| 27   | Grocery               | 68382-792       |          266 |    48.63 |
| 28   | Industrial            | 55711-070       |          753 |    46.48 |
| 29   | Industrial            | 76446-002       |          272 |    89.03 |
| 30   | Jewelery              | 33261-028       |          500 |     66.3 |
| 31   | Jewelry               | 0527-1414       |           68 |    32.81 |
| 32   | Jewelry               | 55111-586       |          297 |    53.61 |
| 33   | Jewelry               | 57664-327       |           46 |    10.28 |
| 34   | Kids                  | 50844-428       |          606 |    20.93 |
| 35   | Kids                  | 60429-038       |          459 |    29.68 |
| 36   | Movies                | 53808-0776      |          278 |    65.75 |
| 37   | Music                 | 42507-158       |          362 |    56.92 |
| 38   | Music                 | 64679-701       |          487 |    28.77 |
| 39   | Outdoors              | 0268-6731       |          335 |    70.04 |
| 40   | Outdoors              | 13107-070       |          257 |     2.71 |
| 41   | Outdoors              | 33261-144       |          809 |     2.95 |
| 42   | Outdoors              | 51672-1330      |          128 |    68.85 |
| 43   | Outdoors              | 63739-141       |          195 |    83.23 |
| 44   | Sports                | 10578-024       |          185 |     2.86 |
| 45   | Sports                | 68151-2870      |          185 |     2.86 |
| 46   | Toys                  | 0245-0709       |          783 |    45.65 |
| 47   | Toys                  | 17089-075       |          484 |    67.69 |
| 48   | Toys                  | 54092-381       |          783 |    45.65 |
| 49   | Toys                  | 64058-413       |          783 |    45.65 |
| 50   | Toys                  | 64092-113       |          450 |    55.23 |
+------+-----------------------+-----------------+--------------+----------+