I m writing a program that validates emails. It gets and email address from a te
ID: 3620164 • Letter: I
Question
I m writing a program that validates emails. It gets and email address from a text file checks for the following to be true with a Boolean function returns true or false then writes the results to another text file. I am a little stumped on how to check for these. I figure I could use if else statements but i don't know how to split the "local part" and "domain" and make sure an @ is between them. Also is there a better way then if else statements. I just need a little point in the right direction. Thank You!!1. Make sure that email length is <= 254
2. Make sure that there is one and only one '@' symbol
email = localPart@domain
3. Make sure that the localPart is <= 64
4. Check localPart for invalid characters
5. Check domain for invalid characters
6. Make sure that localPart does not begin or end with '.' and does not contain '..'
7. Make sure that domain does not begin or end with '.' and does not contain '..'
8. Make sure that localPart does not begin or end with '-' and
does not contain '--'
9. Make sure that domain does not begin or end with '-' and does not contain '--'
domain = sub.sub.tld
10. Make sure that all subdomains are <=64
11. Validate tld against the list of allowed TDLs is extra credit
Explanation / Answer
please rate - thanks should get you started, and then some #include <iostream>#include<string>
#include <fstream>
using namespace std;
bool check(string);
int main()
{
string email;
ifstream in;
ofstream out;
in.open("input.txt");
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
out.open("output.txt");
getline(in,email);
while(in)
{
out<<email<<" ";
if(!check(email))
out<<" is Invalid ";
else
out<<" is Valid ";
getline(in,email);
}
out.close();
in.close();
system("pause");
return 0;
}
bool check(string email)
{int count,i,j,k=0,ast=0,dot=0,size=0,prevsize;
count=0;
for(i=0;i<email[i]!=''; i++)
if(email[i]== '@')
{count ++;
if(count==1)
{ast=i;
size=i;
}
else
return false;
if(i>=64)
return false; //local part>64
j=i;
}
prevsize=size;
count=0;
for(i=0;i<email[i]!=''; i++)
if(email[i]== '.')
{count ++;
if(count==1)
dot=i;
if(i==k+1)
return false; //2 dots next to each other in domain
k=i;
size=i-prevsize;
prevsize=size;
if(size>64)
return false; //subdomain>64
}
if(count!=1)
return false;
if(dot<ast)
return false; //. before *
if(k-j>64||j>=k)
return false;
for(i=0;i<email[i]!=''; i++)
if(!isalnum(email[i])&&email[i]!='@'&&email[i]!='.')
return false; //invalid character
if(i>=255)
return false; //length<=254
return true;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.