C programming Youve gathered lists of email addresses from a
C++ programming
You\'ve gathered lists of email addresses from a variety of sources, and now you want to send out a mass mailing to all of the addresses. However, you don\'t want to send out duplicate messages. All of the email addresses (represented as strings) have been combined on a single file called rawlist.dat. You need to write a program that reads all of the addresses and discards any that have been previously input. Use one of the list classes from this chapter, modifying it as necessary to work with string data, and to deal with as many as 1000 items. After all of the data have been read, output the new mailing list to a file called cleanlist.dat.
Trying to answer this question, i wrote this code, but doesn\'t work
#include<iostream>
#include<fstream>
#include \"rawlist.dat\"
using namespace std;
//To store exam score of 250 students
string examScore[250], len;
//Read exam score from the file
void ReadScore()
{
//Declares an object of ifstream for reading
ifstream rfile;
//Opens the file input.txt in read mode
rfile.open(\"rawlist.dat\");
len = 0;
//Loops till end of the file
while(!rfile.eof())
{
//Reads a character
rfile>>examScore[len++];
}//End of while loop
}//End of read function
//Function to show exam scores
void ShowScore()
{
cout<<\"\ Exam Score \";
//Loops till end of array
for(string x = 0; x < len; x++)
cout<<examScore[x]<<\" \";
cout<<endl;
}//End of show score function
//Function to sort score
void SortScore()
{
string Temp, x, y;
//Loops till end of array
for(x = 0; x < len; x++)
{
//Loops till end of array - current x value - 1
for(y = 0; y < len - x - 1; y++)
{
//Checks if the current exam score is less than the next exam score value then swap
if(examScore[y] < examScore[y + 1])
{
//Swapping
Temp = examScore[y];
examScore[y] = examScore[y + 1];
examScore[y + 1] = Temp;
}//End of if
}//End of for loop for variable y
}//End of for loop for variable x
}//End of function
//Function to display bar chart
void BarChart()
{
//Loops till end of array
for(string x = 0; x < len; x++)
{
//Displays the exam score
cout<<examScore[x]<<\" \";
//Displays number of stars based on the exam score value
for(string y = 0; y < examScore[x]; y++)
cout<<\"*\";
cout<<endl;
}//End of for loop
}//End of barchart function
//Main function
int main()
{
ReadScore();
SortScore();
ShowScore();
BarChart();
}
//rawlist.dat
hello@email.com
hello@email.com
usa@eamil.com
world@email.com
cplusplus@email.com
light@email.com
world@email.com
light@email.com
Solution
Hi
Looks like the code you wrote is intended for some other purpose. It would not remove duplicate mail addresses from a file.
You may use below code for that purpose
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
int main()
{
fstream rawFile;
rawFile.open(\"rawlist.dat\",ios::in);
fstream cleanFile;
cleanFile.open(\"cleanlist.dat\",ios::out|ios::app);
map<string,int> mailMap;
string cur;
while(getline(rawFile,cur)){
if(mailMap[cur]==NULL)
{
mailMap[cur]=1;
cleanFile.write(cur.c_str(),cur.length());
cleanFile.put(\'\ \');
}
else
{
mailMap[cur]++;
}
}
cleanFile.close();
rawFile.close();
return 0;
}


