Problem with checking duplicates

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <algorithm>

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


}

Last edited on
It is rather simple:

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])
Topic archived. No new replies allowed.