I need to create a function that finds duplicates in a file. For example
it is a voting program that needs to detect multiple votes so there are multiple voter IDs which are unimportant. I have the code so far the display the untouched file and now I need to use another function to output the contents into another file.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <algorithm>
usingnamespace std;
//Function for array creation "createarray"
void createarray ()
{
string pirates[5]; // creates array to hold names
short loop; //short for loop for input
short loop2; //short for loop for output
string line; //this will contain the data read from the file
ifstream myfile ("input.txt"); //opening the file.
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line); //get one line from the file
cout << line << endl; //and output it
//criteria for input loop (as array 0-5)
}
}
}
void checkdup ()
{
int mymap;
//Open file once more
string pirates[5]; // creates array to hold names
short loop; //short for loop for input
short loop2; //short for loop for output
string line; //this will contain the data read from the file
ifstream myfile ("input.txt"); //opening the file.
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line); //get one line from the file
cout << line << endl;
//Check for Duplicates
{
fstream myfile;
myfile.open("input.txt",ios::in);
fstream outfile;
outfile.open("counted .txt",ios::out|ios::app);
map <string pirates, int> mymap;
string cur;
while(getline(myfile,cur)){
if(mymap[cur]==NULL){
mymap[cur]=1;
outfile.write(cur.c_str(),cur.length());
outfile.put('\n');
}
else
mymap[cur]++;
}
}
int main()
{
cout <<"Here is the raw data for the votes, these include any duplicates."<<endl;
createarray();
cout << "Here is the file without duplicates"<<endl;
checkdup ();
}
You need to store line int an array (make sure that the index does not exceed the array size!). Before you actually store the data check whether it is already there.
Instead of the array you should use a vector.
A map (map <string pirates, int> mymap;) is a good way to find the duplicates.
Store the data only if(0 == mymap[cur])