email checking function

This is a discussion on email checking function within the C++ Programming forums, part of the General Programming Boards category; I made a email checking function but after i get the message: Email accepted it seems to crash 3 times ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    email checking function

    I made a email checking function but after i get the message: Email accepted it seems to crash 3 times ( the program) what am i doing wrong?

    Code:
    std::string GetValidEmail ()
    {
      std::string email;
      std::cout <<"Your Email address:";
      std::getline (std::cin,email);
      std::string::size_type checkPoint = email.find( ".", 0 );
      std::string::size_type checkAt    = email.find( "@", 0 );
    
      while ( checkPoint == std::string::npos || checkPoint == std::string::npos || email.length() >= 50)
      {
        std::cout <<"Your email is either Too long(max 50 chars) or Invalid.\n"
                  <<"Please input an shorter or valid email adress." << std::endl;
    
        std::getline (std::cin,email);
        checkPoint = email.find( ".", 0 );
        checkAt    = email.find( "@", 0 );
      }
      std::cout <<"Email Accepted" << std::endl;
    }
    and another question : how can i make my email checking function even better? maby implement some kind of list of known emailaddreshosters?

    and if u might need it lol:

    Code:
    int main ()
    {
      std::string kaka;
      kaka = GetValidEmail ();
      std::cout << kaka << std::endl;
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    Just found out the error, it did not return annything (the function) forgot to :

    Code:
    return (email);
    but i could still use some help thinking of ways to make my email function better!

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Maybe you should consider using boost regex to check mail address.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    and what is that supposted to be? :P

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    google("boost regex")[0] = http://www.boost.org/libs/regex/doc/index.html

    It's a regular expression engine. You can tell it to look for
    Code:
    [\w\d]+@[\w\d\.]+*\.[\w\d]+
    and it will match "foo@bar.com" but not "@bar.com" or "foo@bar" etc. My RE isn't perfect, though; it matches "foo@..com" as valid. It's also Perl-style regular expressions so I have no idea if it works with boost::regex.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Ok, here is the regular expression you can use with boost for email matching.

    Code:
    string email_expr = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21