In: Computer Science
*Please write code in C++*
Write a program to verify the validity of the user entered email address.
if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid"
else : output the statement that the email is invalid and list all the violations
ex: "The email sarahwinchester.com is invalid"
* @ symbol
* Missing Domain name
Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
while(true)
{/*Infinet while loop*/
string email;
int
at_pos=-1,dot_pos=-1,i;/*declaring variables*/
cout<<"Enter emailId:";
getline(cin,email);/*Reading email
id*/
if(email=="q")
{/*If q e break the loop*/
break;
}
else
{
if(email[0]>='0'&& email[0]<='9')
{/*if email id
starting with number*/
cout<<"The email id
"<<email<<" is invalid"<<endl;
cout<<"* Email Id should not start with a
number"<<endl;
}
else
{
for(i=0;i<email.length();i++)
{
if(email[i]=='@')
{/*finding the @
index*/
at_pos=i;
}
if(email[i]=='.')
{/*finding the .
index*/
dot_pos=i;
}
}
if(at_pos==-1)
{/*if @ not present*/
cout<<"The email id
"<<email<<" is invalid"<<endl;
cout<<"* @ symbol is
missing"<<endl;
cout<<"* Missing Domain
Name "<<endl;
}
else if(dot_pos==-1)
{/*. not present*/
cout<<"The email id
"<<email<<" is invalid"<<endl;
cout<<"* Missing Domain
Name "<<endl;
}
else if(dot_pos<at_pos)
{/*if dot is not present after @ symbol*/
cout<<"The email id
"<<email<<" is invalid"<<endl;
cout<<"* Missing Domain
Name "<<endl;
}
else
{/*email id is valid*/
cout<<"The email id
"<<email<<" is valid"<<endl;
}
}
}
}
}
Output:
Indentation: