C++ Problem Solving Scenarios - Urgent Help Needed Please

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";

return 0;
}

Last edited on
Show us what you've tried. If you show some effort, we can point out where mistakes are. We can't know what's wrong if you don't show any code.

http://www.cplusplus.com/doc/tutorial/
Sorry about that. The part below is for the email address not containing more than 10 characters:

#include <iostream>
#include <string>
using namespace std;

int main()

{

std:: string email_address [10];
std:: string email = "Please enter your Email ";
std:: string email_address = email + "Address:";
std:: cout << email_address <<std::endl;


}
A string by itself is already a series of characters, so the [10] in your code is actually making an array of 10 strings, which is not what you want.

Just declare it as:
std::string email_address;

And remove your third line. It doesn't make sense, you're re-declaring email_address.

Use std::cin to get input from the user.
See: http://www.cplusplus.com/doc/tutorial/basic_io/

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
}
Last edited on
Thank you so much, will follow your suggestions and see if I can get this running. Thank you again!
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.
Last edited on
@jonnin,
I guess the task is more about string processing than real email validation.
A few hints:
• 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";

return 0;
}

I guess the task is more about string processing than real email validation.

Of course it is. I was just commenting that its a deceptively difficult problem in reality.

OP, that is a good start. you can combine statements to save pointless clutter and cpu work, though.

int elenght = emailaddress.length();
if (elenght > 10)
{

consider instead

if (emailaddress.length() > 10)
{
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.

C++20 is (finally) introducing an ends_with function: https://en.cppreference.com/w/cpp/string/basic_string/ends_with

However, I assume you can't use that.
You need to use something equivalent to ends_with("edu") || ends_with("com") etc.

The easiest way to do this, from what I can tell, is to take the substring of the last three characters.
Look at this example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example program
#include <iostream>
#include <string>
using namespace std;

string get_last_n_chars(const string& str, int n)
{
    if ((int)str.length() < n)
        return "";
        
    return str.substr(str.length() - n);
}

int main()
{
    string str = "another happy landing";
    
    cout << get_last_n_chars(str, 3) << '\n';

    if (get_last_n_chars(str, 7) == "landing")
    {
        cout << "Ends with landing\n";
    }
}

ing
Ends with landing
Last edited on
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.
Thank you for your suggestions. Will do some more reading to better understand.
Topic archived. No new replies allowed.