I wrote a program that is supposed to open a file with a list of emails and outp
ID: 3624531 • Letter: I
Question
I wrote a program that is supposed to open a file with a list of emails and output "1" or "0" for valid email address or invalid email address. I also wrote in code to output what test the address fails. All it does is output all 1's and a random 0 with one of my failure messages. The failure message isn't output to file only screen. But that is another problem I had because I didn't know how to get the "error" messages into the output file. It also somehow outputs more 1's then there are email addresses. I think the latter problem has something to do with the way i defined a maximum length. But I can find out why it outputs only 1's.
Here is the main.cpp and header underneath along with the input file correct output and my output.
#include
#include
#include
#include "EmailValidation.h"
using namespace std;
string email;
void main()
{
//1. Open the input file( Assuming it is in the same directory)
ifstream in("Email.txt");
//2. Open the output file
ofstream out("Result.txt");
//Repeat while not at the end of the input file
while(!in.eof())
// 3. Read the input file
{
getline(in, email);
// 4. Validate email
if (IsEmailValid(email))
{
out << " 1" << endl;
}
else
{
out << "0" << endl;
}
}
out.close();
in.close();
system("pause");
}
HEADER ***********************************************************************
#include
#include
#include
#include
using namespace std;
#define EMAIL_MAX 1000
char apostro = 39;
int const MaxSize = 254;
int const MaxLocal = 64;
int const MaxDomain = 190;
int const MaxNumberOfDomain = 0;
int numberOfTld = 0;
int const NoPosition = -1;
int const Start = 0;
int const Null = 0;
int periodDomainCount = 0;
char charArray[MaxSize];
int periodLocation[MaxSize];
int hyphenLocation[MaxSize];
int periodDomainLocation[MaxSize];
//IsEmailValid checks if the email is valid and sends back a value that is true or false
bool IsEmailValid(string email)
{
// 1. Make sure that email length is <=254 characters
int sizeOfString = email.length();//length() finds the length of the email
if (sizeOfString > MaxSize)//If the size of the string is too large, it will return that is is not valid
{
cout << "[Failed check email size] ";
return false;
}
else
{return true;} // else return true
// 2. Make sure there is only one @ symbol
// email = localPart@domain
int position = email.find_first_of('@');//Finds the @ symbol's position
int lastPosition = email.find_last_of('@');//If there are two @ symbols, this will find
if (( position != lastPosition)||(position == NoPosition)||(position == Start)||(position == sizeOfString - 1))//If @ is in the last variable or is it is the first variable,
{// and checks if there is another @ and will return false for any of these values are ture
cout << " [Failed check @ symbol] ";
return false;
}
else
{return true;}// else return true
// 3. Check Local part length
if((position - 1) > MaxLocal) // Determins how long local part is then returns false if it is bigger then 64 charaters
{
cout << " [Failed check local part length] ";
return false;
}
else
{return true;} // else return true
// 4. Check Domain length
int domainSize = sizeOfString - position; // Size of email address minus local part(up to @ symbol) is domain length
if( domainSize > MaxDomain)
{
cout << " [Failed check domain length] ";
return false;
}
return true; // else return true
// 5. Check local part for invalid chars
int i=Null;
while(email[i]!='@') // while i is not equal to the @ sysmbol perform instruction below
{ // If the email contains anything but below charaters it is invalid
// apostro = '
if(!(email[i] >= 'A' && email[i] <= 'Z') || (email[i] >= 'a' && email[i] <= 'z')
|| (email[i] >= '0' && email[i] <= '9') || (email[i] == '-') || (email[i] == '_') || (email[i] == '.')
|| (email[i] == '+') || (email[i] == '*') || (email[i] == '^') || (email[i] == apostro) || (email[i] == '#')
|| (email[i] == '$') || (email[i] == '!') || (email[i] == '%') || (email[i] == '&') || (email[i] == '/')
|| (email[i] == '=') || (email[i] == '?') || (email[i] == '`') || (email[i] == '{') || (email[i] == '|')
|| (email[i] == '}') || (email[i] == '~'))
{
i++;
cout << " [Failed check for valid characters in local part] ";
return false;
}
else
return true; // else return true
}
// 6. Check that local part does not start or end with '.' or '-'
if ((email[Null] == '.') || (i > Null && email[i-1] == '.') || (email[Null] == '-') || (i > 0 && email[i-1] == '-'))
{cout << " [Failed check for the starting/ending charater . and - within local part] ";
return false;}
return true; // else return true
// 7. Check domain for invalid characters
int atPosition = lastPosition;
i = Null;
while(email[i] = atPosition)
{
if(!(email[i] >= 'A' && email[i] <= 'Z') || (email[i] >= 'a' && email[i] <= 'z')
|| (email[i] >= '0' && email[i] <= '9') || ( email[i] == '-') || (email[i] == '.'))
{
cout << " [Failed check for valid domain charaters] ";
return false;// if it is not any of the above return false(invalid)
i++;
}
return true; // else return true
}
// 8. Check that domain does not start or end with '.' and '-'
if(email[atPosition + 1] == '.')
{return false;}
if(atPosition > 0 && email[MaxSize - 1] == '.')
{return false;}
if(email[atPosition + 1] == '-')
{return false;}
if(atPosition > Null && email[MaxSize - 1] == '-')
cout << "[Failed check for the starting/ending charater . and - within domain] ";
{return false;}
{return true;} // else return true
// 9. Make sure that the local part does not contain ".." or "--" or "-." or ".-"
i = Null;
while(email[i]!='.')// Checks for first dot in local part
{
i++;
if(email[i] == '@')// Once @ is reached and no dots are found return true
{return true;}
}
i++;
int positionAfterDot = i++;
if((positionAfterDot == '.') || (positionAfterDot == '-'))//Checks for second dot or a dash in local part
{return false;}
i = 0;
while(email[i] != '-')
{
i++;
if(email[i] == '@')// Once @ is reached and no dashes are found return true
{return true;}
}
i++;
int positionAfterDash = i++;
if((positionAfterDash == '-') || (positionAfterDot == '.'))// Checks for second dash part or a dot in local part
{return false;}
// 10. Make sure that the domain does not contain ".." or "--"
while(email[atPosition + 1] !='.')
{
i++;
}
i++;
positionAfterDot = i++;
if((positionAfterDot == '.') || (positionAfterDot == '-'))
{return false;}
while((positionAfterDot != '.') || (positionAfterDot != '-'))
{
i++;
if((positionAfterDot == '.') || (positionAfterDot == '-'))
{return false;}
}
// 11. Make sure that all subdomains for <=64 characters
// domain = sub.sub.tld
int lastDot = email.find_last_of('.');
int tld;
tld = sizeOfString - lastDot;
if ((tld <= 1) || (tld > 4) || (lastDot == -1) || (lastDot > atPosition))//If the last period doesnt exist or the domain is less than 2, this will return false
{return false;}
// make sure tld is less then 4 characters
/*
//12. Extra credit. match up a list of top level domain names from a file and validate with the email.
int numberOfTld = Null;
ifstream infile("tld.txt");//Takes the top level domains from a single text file and tests if the emails correspond to them
while(!infile.eof())
{
string tld;//creates a new string for the top level domains
getline(infile, tld);//Takes the whole line from the files
string domainname;//Creates a new string for the domain of the email string to go into
domainname = email.substr(lastDot, sizeOfString);//splits up the email string and takes the tld domain from it
// and places it into the domain name string
numberOfTld++;// Cycles through the tld
if(numberOfTld == MaxNumberOfDomain)//This cycles through the possible 269 possible domain names, if none match, then this is false
{return false;}
if(domainname == tld)//If the strings match, then the email will be complete and be true
{return true;}
}
return true;*/
};
Here are the emails in the "email.txt" file.
fomitchev@adelphia.net
mif10@cse.psu.edu
@psu.edu
psu.edu
max.fomitchev@
max.fomitchev@psu.edu
max-fomitchev@psu.edu
max@ultramax-music.com
max@fomitchev@psu.edu
max@.psu.edu
i.love.you@happy.valley.net
i-love-you@happy.valley.mil
i_love_you@happy.valley.org
i..love@happy.valley.net
i.love@happy..valley.org
i.love@happy.valley.commercial
user@domain1.domain2.domain3.domain4.dom5.d6.7.finaldomain.org
max@home.c
max@home.
max@home
max @home.com
max@h ome.com
.max@psu.edu
max.@psu.edu
fomitchev@-psu.edu
fomitchev@psu-.edu
max"fomitchev@psu.edu
max:fomitchev@psu.edu
max>fomitchev@psu.edu
max
max(fomitchev@psu.edu
max)fomitchev@psu.edu
max,fomitchev@psu.edu
max;fomitchev@psu.edu
max[fomitchev@psu.edu
max]fomitchev@psu.edu
maxomitchev@psu.edu
o'henry@writer.com
max|fomitchev@psu.edu
I+am+sad@amazon.co.uk
too-long-of-a-user-name-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa65@domain.com
user@too-long-of-a-domain-name-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa256.com
Here is the correct output:
1
1
0
0
0
1
1
1
0
0
1
1
1
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
And this is what I get:
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
I know its a lot to look at but I've been trying to figure out whats wrong by myself and cannot.
Explanation / Answer
Please give me the points I really need them :) I appreciate :) I will return the favor ;)