Email parser problem

Hello. I'm having problems making a program read a text file and skip emails that consist of two consecutive periods in the local and domain parts of the email(local-part@domain). The program still counts emails such as "..@.." and "..Him..@gmail.com" as good emails. Is there a way for me to make the program skip emails that have two consecutive periods?

Here's what I have so far:

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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

#include <cctype>

// conversion to uppercase
class toUpper {public: char operator()(char c) const {return toupper(c);}};

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


int main()
{
// declare constants (default file names)
const string DEFAULT_INPUT_FILE = "fileContainingEmails.txt";
const string DEFAULT_OUTPUT_FILE = "copyPasteMyEmails.txt";

// declare array list
const int MAX_EMAILS = 1000;
int nEmails = 0;
string email[MAX_EMAILS];

// declare variables
string inputFileName;
string outputFileName;
ifstream fin; // file pointers
ofstream fout;

string line; // line from file
int s = 0, // start position of email
e = 0; // end position of email
bool hasDot = false; // check for period after @ symbol
string aEmail; // an email address!
bool emailAlreadyExists = false;
string email1, email2; // temporary strings, for converting emails to uppercase
string temp; // temporary string for sorting


// prompt user for input filename
cout << "Enter input filename [default: " << DEFAULT_INPUT_FILE << "]: ";
getline(cin, inputFileName);

if (inputFileName.length() == 0) {
inputFileName = DEFAULT_INPUT_FILE;

// prompt user for output filename
cout << "Enter output filename [default: " << DEFAULT_OUTPUT_FILE << "]: ";
getline(cin, outputFileName);

if (outputFileName.length() == 0) outputFileName = DEFAULT_OUTPUT_FILE;

} else {
// prompt user for output filename
cout << "Enter output filename [default: " << inputFileName << "]: ";
getline(cin, outputFileName);

if (outputFileName.length() == 0) outputFileName = inputFileName;
}


cout << endl;
cout << "Input file = " << inputFileName << endl;
cout << "Output file = " << outputFileName << endl;
cout << endl;

// open input file
fin.open(inputFileName.c_str());
if (!fin.good()) throw "I/O error";

while (fin.good())
{
getline(fin, line); // read a line

for (int i = 0; i < line.length(); i++) {

// look for '@' character
if (line[i] == '@') {

// Then look backwards in a loop until you find an invalid email address
// character, or run into the start of line.
s = i;
while (s > 0 && isValidEmailCharacter(line[s-1])) {
s--;
}

// Then look forwards from the same '@' in another loop until you find
// an invalid email address character, or run into the end of line.
e = i;
hasDot = false;
while (e < line.length() && isValidEmailCharacter(line[e+1])) {
if (line[e] == '.') hasDot = true;
e++;
}

// check for valid email
if (s < i && e > i && hasDot) {

aEmail = line.substr(s, e-s +1);

// check for duplicates
emailAlreadyExists = false;

email1 = aEmail;
transform(email1.begin(), email1.end(), email1.begin(), toUpper());

for (int i = 0; i < nEmails; i++) {
email2 = email[i];
transform(email2.begin(), email2.end(), email2.begin(), toUpper());
if (email1 == email2) emailAlreadyExists = true;
}

// if non-duplicate, add to list if there is space
if (!emailAlreadyExists && nEmails < MAX_EMAILS) {
email[nEmails++] = aEmail;

// print email address
cout << aEmail << endl;
}
}
}

}
}
cout << endl;

// close input file
fin.close();

// if one or more emails are found
if (nEmails > 0) {

// open output file
fout.open(outputFileName.c_str());
if (!fout.good()) throw "I/O error";

// write emails to file
for (int i = 0; i < nEmails; i++) {
fout << email[i];
if ((i+1) < nEmails) fout << "; ";
}

// close output file
fout.close();

cout << nEmails << " non-duplicate email " << ((nEmails==1) ? "address was" : "addresses were") << " found, and copied to the file " << outputFileName << endl;
} else {
cout << "Sorry, no email addresses were found in the file " << inputFileName << endl;
}

return 0;
}


Any help would be appreciated. Thank you!
It's rather difficult to follow the code. There is no indentation. There are exceptionally long lines. And main() is an exceptionally long function. It is important to clearly format code if you want people to comprehend and help critique your code.

I recommend you create at least two functions that can be independently tested: isValidAddress() and isDuplicateAddress().

Where in your code do you check for a double dot?
Topic archived. No new replies allowed.