Email Parser

I have been trying to debug this program but its really given me a nightmare, i will appreciate if you guys can look and advise what i am doing wrong.

the program run and print out emails from and input files but since i add a another code to check for duplicate, it will only print out the first email in a line and proceed to the second line instead of it to continue to check on the lines.

but if i comment out the check duplicate portion, it print out all the valid email as defined.

thanks
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  
//libraries
#include<algorithm>
#include <deque>
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
#include<cctype>

struct EmailList
{
  string anEmail;
}; // EmailList

// required for conversion to lowercase 
class toLower {public: char operator()(char c) const {return tolower(c);}};

//Programmer defined data types
//NONE

//Special compiler dependent definitions
//NONE

//global constants/variables
//NONE

//Programmer defined functions
void introduction()
{
 // output my name and objective and program information
 cout << "The purpose of the program is to extract email addresses embedded in the first (input) text file,and copy them to the second (output) text file.\n"; 
 cout << "Programmer: Toraaglobal\n"; 
 cout << "Editor(s) used: Notepad\n"; 
 cout << "Compiler(s) used: MinGW\n"; 
 cout << "File: " << __FILE__ << endl; 
 cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl; 
} // introduction

 bool isValidEmailCharacter (char c)  
{
 bool result = false;
 if (c == '_' || c== '.') result = true;
 if (c == '-' || c == '+') result = true;
 if (c >='a' && c <= 'z' )result = true;
 if( c >= 'A' && c <= 'Z')result = true;
 if (c >= '0' && c<= '9') result = true;
 return result;
}//isValidEmailCharacter

bool hasDot(char c)
{
 bool result = false;
 if(c=='.') result = true;
 return result;
}//hasDot


//main program
int main()
{
  //program introduction
  introduction();
 
  //variables
  string dFilename;   // default input file name
  string IFile;   // the user input file name using the console
  string OFile;  // the user output file name using the console
  string userrespond;   // user respond to continue
  ifstream fin ;  // required for file input
  ofstream fout;  // required for file output
  string line;  // each line in the input file
  int i=0;  // loop counter
  int s ;  // to count backward until a non valid character is found
  int e ; // to count forward until a non valid character is found
  bool isdot;   //use to confirm a dot 
  int numberOfEmail = 0;
  bool dupe;
  string temp;     // to temporary store the valid email to check for duclicate
  string temp2;   // use to check for duclicate
  EmailList aEmailList;
  string validEmail;
 

  // promt user for file input or use default input file
   dFilename = "fileContainingEmails.txt";
   cout<<"Enter input filename [default: fileContainingEmails.txt]: "<<endl;
   getline(cin,IFile);
   
   if(IFile.length() ==0)
   {
     IFile = dFilename;
   }
   else
   {
     IFile = IFile;
   }
  // promt the user for output file
   dFilename = "copyPasteMyEmails.txt";
   cout<<"Enter output filename [default: copyPasteMyEmails.txt]: "<<endl;
   getline(cin,OFile);
   if(OFile.length() == 0)
   {
     OFile = dFilename;
   }
   else
   {
     OFile =OFile;
   }
   // output user working files
   cout<<"Your input file name is : "<<IFile<<endl;
   cout<<"Your output file name is : "<<OFile<<endl;

   // promt the user to continue the program
    cout << "Press Enter Key to continue: "; 
    getline(cin,userrespond); 
    

   // open the input file
   fin.open(IFile.c_str());
   if(!fin.good())cout<<"Invalid file : "<<IFile<<endl;
 
   deque<EmailList> emailList; // create an emty list
   

   // read the input file
   while(fin.good())
   {
      getline(fin, line);
      for(i=0; i< line.length() ; i++)
      {
         if (line[i] =='@')   
         {
             for( s= i-1;s > -1 ; s--)
             { 
                 if(isValidEmailCharacter(line[s]) == false)break;
             }//for      
             s = s+1;
             isdot = false;
             for(e = i+1; e < line.length(); e++)
             { 
                 if(!isdot) isdot = hasDot(line[e]);
                 if(isValidEmailCharacter(line[e]) == false)break;
             }//for
                 
             if(s<i && e>i && isdot )
             {
                 temp = line.substr(s, e-s);
                 validEmail = line.substr(s, e-s);
                 dupe=false;
                  
                 for(i = 0 ; i< emailList.size();i++)
                 {
                     temp2= emailList[i].anEmail;
                     transform(temp.begin(), temp.end(), temp.begin(), toLower());
                     transform(temp2.begin(), temp2.end(), temp2.begin(), toLower());
                     if(temp == temp2) dupe=true;    
                 }//for
                 
                  if(dupe==true)break;
                  aEmailList.anEmail = validEmail;
                  emailList.push_back(aEmailList); 
             } 
        } 
      }//for close  
    }// while close
    fin.close();
   if(emailList.size() == 0)
    {
      cout<<endl;
      cout<<"Sorry, no email addresses were found in the "<<IFile<<endl; 
    }//if
    else
    {
       cout<<endl;
       fout.open(OFile.c_str());
       if(!fout.good())throw "I/O error"; 
       for(i=0; i < emailList.size();i++)
       {
         cout<<emailList[i].anEmail<<endl;
         fout<<emailList[i].anEmail<<"; ";
       }
       cout<<endl;
       cout<<emailList.size()<<" email addresses were found, and copied to the  "<<OFile<<endl;
       cout<<endl;
       cout<<"OPen the "<<OFile<<" and copy and paste the email address to the cc or bcc,";
       cout<<" but bcc will be better because others will not see the list of all the recipient"; 
       cout<<" which also protect the privacy of individual "<<endl;
       fout.close();
    }//else
     
} //main 
if your the input file contain the following email
rburns@dvc.edu; x@dvc.edu; y@dvc.edu; z@dvc.edu; rdb3@rdb3.com; tina@rdb3.com; rdb3@com.com



if all the email are in one line, it will only print the first email and ignore other email. but if i comment out
dupe=false;

for(i = 0 ; i< emailList.size();i++)
{
temp2= emailList[i].anEmail;
transform(temp.begin(), temp.end(), temp.begin(), toLower());
transform(temp2.begin(), temp2.end(), temp2.begin(), toLower());
if(temp == temp2) dupe=true;
}//for

if(dupe==true)break;

it will print all the email in the file
Your program to check if an email id exists is unnecessarily complicated, using std::find() would make things much

simpler. Also breakup the string line as soon as it is read from the file and work with the individual strings inside line which are more tractable:
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    fstream fin("D:\\input.txt");
    ofstream fout("D:\\output.txt");
    vector<string> emailList;
    int i{};

    while(fin)
    {
        string email;
        fin >> email;
        if(fin)
        {
            emailList.push_back(move(email));
        }
    }
    for (auto& elem : emailList)
    {
        if(elem.find('@') != string::npos)//an email id will have an '@'
        {
            if(elem.back() == ';')//and all but the last email is separated by ';'
            {
                elem.pop_back();//we remove the ';'
                fout << elem << '\n';
                i++;
            }
            else
            {
                fout << elem << '\n';//the last email doesn't have a ';'
                i++;
            }
        }
    }
    cout << "Number of emails found: " << i << '\n';
}

Topic archived. No new replies allowed.