1. Write a program that asks the user to input an email address. Check whether the email address is valid or not. The conditions to be checked are:
• Email address should not contain more than 10 characters.
• Email address should not contain spaces.
• Email address should contain @ in it.
• The last three characters should either be “com” or “edu” or “org”.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string emailaddress;
cout <<"Please enter an email address :";
getline(cin, emailaddress);
int elenght = emailaddress.length();
if (elenght > 10)
{
cout <<"Your email must NOT contain more than 10 characters, please try again"<<endl;
return 0;
}
int sp = emailaddress.find(" ");
if (sp > 0)
{
cout << "Your email must NOT contain any spaces, please try again" << endl;
return 0;
}
int at = emailaddress.find("@");
if (at == string::npos)
{
cout << "Your email MUST contain an '@' sign, please try again" << endl;
return 0;
}
int end1 = emailaddress.rfind("com", "edu", "org" );
if (end1 == string::npos)
{
cout << "Please enter an email domain from the following list - com, edu, org" << endl;
return 0;
}
else
cout<< emailaddress << " Email Address not valid";
To check the length of a string, use email_address.length()
You're going to want if statements to verify the rules of what you're checking for,
see: http://www.cplusplus.com/doc/tutorial/control/
To check if a string contains a particular character, you can do
1 2 3 4 5 6 7 8 9
std::string my_str = "blah@bleh";
if (my_str.find("@") != std::string::npos)
{
// at sign found
}
else
{
// at sign not found
}
1. Write a program that asks the user to input an email address. Check whether the email address is valid or not.
ironically that is a very difficult real world challenge that manager/nontechnical people can't understand just how hard it actually is. The rules you have are not correct in real life by a long shot. Its a good real world problem to think about, even with the simplified rule set.
there is a substring function that can take the last 3 letters, then you can just check equality.
@@@.edu appears to be legal with your rule set. Hehe.
so is derp@edu
is upper case EDU etc ok? may want to watch for case on those, or ask your prof.
• Email address should not contain more than 10 characters.
The string class has a size() method that returns the number of characters in the string.
• Email address should not contain spaces.
The string class has a find() method that will find the position of a character in a string. It returns string::npos if the character isn't found.
• Email address should contain @ in it.
Use the find() method again.
• The last three characters should either be “com” or “edu” or “org”.
The string class has a substr() class that returns part of a string. Use it to extract the last 3 characters. You'll need to do a little math to figure out how to get the last 3. http://www.cplusplus.com/reference/string/string/substr/
Thanks everyone, here is what I did so far, it's not complete but it's a start:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string emailaddress;
cout <<"Please enter an email address :";
getline(cin, emailaddress);
int elenght = emailaddress.length();
if (elenght > 10)
{
cout <<"Your email must NOT contain more than 10 characters, please try again"<<endl;
return 0;
}
int sp = emailaddress.find(" ");
if (sp > 0)
{
cout << "Your email must NOT contain any spaces, please try again" << endl;
return 0;
}
int at = emailaddress.find("@");
if (at == string::npos)
{
cout << "Your email MUST contain an '@' sign, please try again" << endl;
return 0;
}
int end1 = emailaddress.rfind("com", "edu", "org" );
if (end1 == string::npos)
{
cout << "Please enter an email domain from the following list - com, edu, org" << endl;
return 0;
}
else
cout<< emailaddress << " Email Address not valid";
In my opinion, that shouldn't be what a beginner needs to worry about; in fact both versions would probably be compiled to the same thing.
StacyG, what's more flagrant is int end1 = emailaddress.rfind("com", "edu", "org" );
Did you read documentation for rfind? I don't even think that would compile.
In my opinion, that shouldn't be what a beginner needs to worry about; in fact both versions would probably be compiled to the same thing.
the visual clutter and extra typing are worth cutting out even as a beginner. Its minor and not the focus, agreed, but the less clutter in code, the easier it is to read and debug, at least to me.
StacyG, nice code! It's refreshing to see a beginner who catches on so quickly.
Please use code tags when posting. Edit your post, highlight the code and click the "<>" button to the right of the edit window.
1 2 3 4 5 6
int sp = emailaddress.find(" ");
if (sp > 0)
{
cout << "Your email must NOT contain any spaces, please try again" << endl;
return 0;
}
This has a couple of problems. First, what if the string starts with a space? Remember that in C++, indexes start with 0, not 1.
Also, find() will return the magic number string::npos if the value isn't found. What if that number is positive? The only reliable way to test if something is found is to compare it to string::npos, so the if should be : if (sp != string::npos)
Also, because find returns a number of type size_t, you should declare sp as a size_t: size_t sp = emailaddress.find(" ");
After this, the only change needed is the one discussed above to compare against the last 3 characters.