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

C++ program involving file IO. I have a database file with comma separated value

ID: 3710078 • Letter: C

Question

C++ program involving file IO. I have a database file with comma separated values. The values are in the format (index, department, item code, quantity, cost). There are duplicates in this database file. Read in the database, remove the duplicates, and write a new comma separated database file without the duplicates. The index field in the new file must be sequential and start at one. Do not design the program to the provided data file. The submitted program will be run against other files of the same format.

Program tasks:

Prompt the user for the database file name. This file name is the path and the file name. For example "/home/folder/database.csv". Do not assume the file is in a particular directory. Use a single prompt.
Open the database
Read in the records using a custom class to hold a record
Remove the duplicates. Note: A duplicate is identified by all four fields being the same
Write the scrubbed database as a new CSV file starting with 'new_' and the input database file name.
Write the new file to the same directory that the database file was read from. For example "/home/folder/new_database.csv"
The index field in the new file must be sequential and start at one.

Some suggestions:

Create a class to hold a single data record.

Override the == operator to ease comparison and override the << operator to output the comma separated values.
Use the class constructor to receive a line from the database then parse the values or override the >> input operator.
Modify the string stream getline() delimiter to a comma.

Use a vector to contain the data records.
While the provided database file contains valid data be aware of potential errors from bad data records.
Do not write the program just for the attached data file.

Here is the database file:


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:

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();
return 0;
}

Record.h:


#ifndef RECORD_H_
#define RECORD_H_

//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 "Record.h"

using namespace std;

Record:Record(string s){


}

Record::~Record*(){


}

Explanation / Answer

Given below is the completed code for the question.
Please do rate the answer if it was helpful. Thank you


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

output file: new_database.csv
----------------------------
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,Baby,76519-1003,430,16.24
24,Games,65342-1393,496,69.07
25,Electronics,0409-9630,797,92.55
26,Jewelery,33261-028,500,66.3
27,Computers,66336-058,375,84.16
28,Outdoors,51672-1330,128,68.85
29,Beauty,48951-8130,725,83.42
30,Baby,59779-224,848,82
31,Industrial,55711-070,753,46.48
32,Industrial,76446-002,272,89.03
33,Sports,68151-2870,185,2.86
34,Toys,0245-0709,783,45.65
35,Games,49999-963,523,93.65
36,Beauty,52125-508,500,2.38
37,Toys,54092-381,783,45.65
38,Beauty,55154-6649,666,79.52
39,Jewelry,57664-327,46,10.28
40,Grocery,49738-453,317,29
41,Grocery,68382-792,266,48.63
42,Outdoors,0268-6731,335,70.04
43,Beauty,68788-9494,12,16.81
44,Outdoors,33261-144,809,2.95
45,Computers,0641-6040,104,88.13
46,Automotive,0781-3059,577,95.24
47,Kids,60429-038,459,29.68
48,Sports,10578-024,185,2.86
49,Jewelry,55111-586,297,53.61
50,Automotive,30142-289,282,33

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote