Instructions
Objective
Write a C++ assignment program to create unique identifier for csv.
Requirements and Specifications
Revision Questions
- Each day Acme ltd, receive multiple “leads” from their sales reps around the country. As the sales reps are given bonuses based on the leads they bring in, they sometimes submit duplicate leads, or they try and submit other reps leads as their own.
To help catch these issues, you are required to write an application that will accept two CSV file containing leads (e.g., john_leads.csv and jane_leads.csv) and output three separate CSV files (common.csv and unique.csv) where one file contains the leads that are common (e.g., to john and jane), the second file contains the leads that are unique to john, and the third file contains the leads that are unique to jane. The phone number of the lead will be used as a unique identifier for each lead. This then allows the company to count how many unique leads each sales rep has submitted and to award the appropriate bonus. It also allows the company to see how many duplicate leads were submitted for the two reps.
Source Code
#include
#include
#include
#include
#include
using namespace std;
// Create a Struct named Lead to store info about all LEADS
struct Lead
{
string first_name;
string last_name;
string description;
string phone_number;
};
int main()
{
// Define vectors to store structs of type Lead from files
vector first_leads;
vector second_leads;
// Define structs to store unique leads for each file, and common ones
vector first_unique;
vector second_unique;
vector common;
// Ask user for files
string file1;
string file2;
cout << "Enter the name of the first file: ";
getline(cin, file1);
cout << "Enter the name of the second file: ";
getline(cin, file2);
// create vector object to store each row elements
vector row;
/* Read first file */
string line; // variable to store each line
string word;
// open first file
ifstream firstFile(file1);
if (firstFile.is_open())
{
while (getline(firstFile, line))
{
row.clear();
stringstream s(line);
while (getline(s, word, ','))
{
row.push_back(word);
}
Lead lead;
lead.first_name = row[0];
lead.last_name = row[1];
lead.description = row[2];
lead.phone_number = row[3];
first_leads.push_back(lead);
}
firstFile.close();
}
else
cout << "Could not open file: " << file1 << endl;
// open second file
ifstream secondFile(file2);
if (secondFile.is_open())
{
while (getline(secondFile, line))
{
row.clear();
stringstream s(line);
while (getline(s, word, ','))
{
row.push_back(word);
}
Lead lead;
lead.first_name = row[0];
lead.last_name = row[1];
lead.description = row[2];
lead.phone_number = row[3];
second_leads.push_back(lead);
}
secondFile.close();
}
else
out << "Could not open file: " << file1 << endl;
cout << "Read " << first_leads.size() << " leads from first file and " << second_leads.size() << " from second file" << endl;
// Now, analyze the leads from first file
bool found;
for (int i = 0; i < first_leads.size(); i++)
{
Lead leadi = first_leads[i];
found = false;
// Iterate through the leads from second file
for (int j = 0; j < second_leads.size(); j++)
{
Lead leadj = second_leads[j];
// Check if there is a lead in from second file that is also in the first file
if (leadi.phone_number.compare(leadj.phone_number) == 0)
{
found = true;
break;
}
}
if (!found) // Lead i (first file) is not in second file, so it's unique for first file
{
first_unique.push_back(leadi);
}
else // It's not unique, it is common
{
// first, check if this lead is already in the common vector
bool found = false;
for (int k = 0; k < common.size(); k++)
{
Lead lead = common[k];
if (lead.phone_number.compare(leadi.phone_number) == 0)
{
found = true;
break;
}
}
if (!found) // not in common vector
common.push_back(leadi);
}
}
// Now, do the same but with second file
for (int i = 0; i < second_leads.size(); i++)
{
Lead leadi = second_leads[i];
found = false;
// Iterate through the leads from first file
for (int j = 0; j < first_leads.size(); j++)
{
Lead leadj = first_leads[j];
// Check if there is a lead in from second file that is also in the first file
if (leadi.phone_number.compare(leadj.phone_number) == 0)
{
found = true;
break;
}
}
if (!found) // Lead i (second file) is not in first file, so it's unique for second file
{
second_unique.push_back(leadi);
}
else // It's not unique, it is common
{
// first, check if this lead is already in the common vector
bool found = false;
for (int k = 0; k < common.size(); k++)
{
Lead lead = common[k];
if (lead.phone_number.compare(leadi.phone_number) == 0)
{
found = true;
break;
}
}
if (!found) // not in common vector
common.push_back(leadi);
}
}
cout << "There are " << first_unique.size() << " unique leads in first file, " << second_unique.size() << " unique leads in second file and " << common.size() << " common leads." << endl;
// Now, write output files
string output_file1_name = "file1_unique.csv";
ofstream outputFile1(output_file1_name);
for (int i = 0; i < first_unique.size(); i++)
{
Lead lead = first_unique[i];
outputFile1 << lead.first_name << "," << lead.last_name << "," << lead.description << "," << lead.phone_number << endl;
}
outputFile1.close();
string output_file2_name = "file2_unique.csv";
ofstream outputFile2(output_file2_name);
for (int i = 0; i < second_unique.size(); i++)
{
Lead lead = second_unique[i];
outputFile2 << lead.first_name << "," << lead.last_name << "," << lead.description << "," << lead.phone_number << endl;
}
outputFile2.close();
string output_common_name = "common.csv";
ofstream outputFile(output_common_name);
for (int i = 0; i < common.size(); i++)
{
Lead lead = common[i];
outputFile << lead.first_name << "," << lead.last_name << "," << lead.description << "," << lead.phone_number << endl;
}
outputFile.close();
}
Similar Samples
Dive into our programming assignment samples at ProgrammingHomeworkHelp.com, showcasing solutions across diverse languages and topics. From encryption algorithms in Assembly Language to database queries in SQL, each sample exemplifies our expertise in delivering precise and efficient solutions. Explore our samples to discover how we can assist you in mastering complex programming challenges.
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++