Strings, Email verification

I have this code that is supposed to verify email adresses and i have nearly perfected it. The problem with it is that the period can be anywhere and my program will say its valid. For example if i input Albert.var@gmail then it will mark it as correct. Help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
#include <string>
#include <cctype>
using namespace std;

bool isemail(std::string const& address) //testing for @
{
    size_t at_index = address.find_first_of('@', 0);
    return at_index != std::string::npos;
}
bool isdot(std::string const& address)// testing for the dot
{
    size_t at_in = address.find_first_of('.', 0);
    return at_in != std::string::npos;
}
int main()
{
  string adress;
  cout << "Enter your email address\n";
  cin >> adress;
if (!isemail(adress))
  {
      cout << "Missing @ symbol\n";
  }
else if (!isdot(adress))
{
    cout << "Missing . symbol after @\n";
}
else
{
    cout << "Email accepted.\n";
}
  return 0;
}
Is this for fun or do you actually need to verify the validity of the address? The correct way to verify an email address is to check if it matches the regular expression in RFC822: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
Its for scholastic purposes. Im pretty sure im just over thinking something
1
2
3
4
5
6
7
8
9
10
11
// rudimentary. see: https://en.wikipedia.org/wiki/Email_address#Syntax
bool valid( std::string address )
{
    std::size_t at = address.rfind('@') ; // find the last '@'
    if( at == std::string::npos ) return false ; // no '@'

    std::size_t dot = address.find( '.', at ) ; // find '.' after the last '@'
    if( dot == std::string::npos ) return false ; // no '.' after the '@'

    return dot < address.size() - 1 ; // true if there is at least one character more, after the dot
}
Topic archived. No new replies allowed.