Thread: Entering an email...the correct way!

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    37

    Entering an email...the correct way!

    I was working on a project, and I was wondering if I can make something like a email entering system, probably using a class. I want it to do the following -
    1. First perform a prelim check for any invalid special characters.
    2. Perform a check for the @ sign (which should be present only once), and atleast one or more periods ('.')
    Its easy to check for point 1, but I dont know how to accomplish point 2 simultaneously. Even one @ sign would be easily 'caught' in the first check, and if I make that an exception (by not putting it in the list for checking symbols), then multiple instances of the '@" will be possible...not realistic.
    Please help

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >and atleast one or more periods ('.')

    email addresses do not need a period. perspective@net is a valid email address on a top level domain. Most top level domains don't give out email addresses though, but some do.

    The best way to validate an email address would be to use a regular expression. Failing that, you can implement you're point two by keeping track of how many '@' symbols you've seen while you do your scan.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered using a regular expression library such as the PCRE library, or the one provided by Boost?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It is extremely hard, and just might be impossible, to write a regular expression that correctly handles every valid e-mail address as specified by RFCs 2821 and 2822.

    The Wikipedia article is pretty good.
    http://en.wikipedia.org/wiki/E-mail_address
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Using purely your rules as provided:
    Code:
    #include <algorithm>
    #include <string>
    ...
    std::string email;    // Get input from user for email address and put here
    std::string special;  // Add whatever invalid characters you want here
    ...
    if( std::count(email.begin(),email.end(),'@') != 1 ||
        std::count(email.begin(),email.end(),'.') == 0 ||
        email.find_first_of(special) != std::string::npos )
    {
        // Invalid address
    }
    else
    {
        // Valid address
    }
    If you're just trying something simple out then that should work. But yeah, in reality the rules (what's a valid/invalid address) are more complex.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    It is extremely hard, and just might be impossible, to write a regular expression that correctly handles every valid e-mail address as specified by RFCs 2821 and 2822.

    The Wikipedia article is pretty good.
    http://en.wikipedia.org/wiki/E-mail_address
    It is possible. http://ex-parrot.com/~pdw/Mail-RFC822-Address.html

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Note quite:
    This regular expression will only validate addresses that have had any comments stripped and replaced with whitespace (this is done by the module).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Code:
    class Email
    {
      char email[20];
      int count=0;
     public:
      void enter_email();
    };
    Email::enter_email()
    {
      cout<<"\nEnter email...";
      cin.getline(email,20);
      for(int i=0; email[i]=='\0'; i++)
      {
         if(email[i]=='@')
          count++;
      }
      if(count!=1)
      {
        cout<<"\nInvalid email. Please try again.";
        enter_email(); //I hope this wont cause trouble if I make multiple objects? O.o
        count=0;
      }
    }
    I hope that cuts it, and more simply so o_o

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Recursion is a poor solution for this particular type of code, don't you think?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yep. Enter enough invalid email addresses, (a few thousand or ten thousand, but that's no trouble for a little script) and your app crashes.

    Oh, and 19 effective characters is awfully little. My short email address has 18, my long one 29. And I've seen far longer addresses.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, even my short e-mail address [an alias through a friendly web-site] is longer than 20. My work-email is 27 characters [not counting the end-zero], and my "actual" mail-address that the alias leads to is simiarl length. webmaster(at)cprogramming(dot)com would also be well beyond the limit.

    Any e-mail that is "[email protected]", given that most names are 3-10 letters each side of the dot would mean that a "common" name would easily reach 30-40 letters. I'd say e-mail addresses should be alloweed to be at least 50 chars, just to cope with unusually long, double-barrel names and such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Just use a std::string and have it be nearly any length. (But data input is subject to denial of service through out of memory conditions, so limit it at a 1000 characters or so.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Uh, length shouldnt be a problem at THIS stage, I guess, its just experimental -_-
    Whatever is recursion? O.o
    And this app is meant for a school project...if I were making something for myself, I'd prolly make it something better and longer and stronger, but I suppose no point getting an army for killing a worm. o.o
    And how dyou limit characters in std::string?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ultrabot90 View Post
    Uh, length shouldnt be a problem at THIS stage, I guess, its just experimental -_-
    Whatever is recursion? O.o
    And this app is meant for a school project...if I were making something for myself, I'd prolly make it something better and longer and stronger, but I suppose no point getting an army for killing a worm. o.o
    And how dyou limit characters in std::string?
    You don't limit it "in std::string", you limit it in getline().
    http://www.cppreference.com/cppstring/getline.html

    Recursion is when you call your own function - which is fine if:
    1. You know that there will not be an "infinite" number of calls.
    2. It actually helps solve the problem. In this case, it just avoids using a while/for/do-while loop construction.

    In your case, #2 is definitely broken, and #1 is not completely impossible.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    you call enter_email from inside enter_email, if a entered string is an invalid email-address (after your definition) to try the next one. thats recursion. silly one, not needed and dangerous, because every new recursion step puts things on the stack of your process and the stack is very limited.
    Better transform the algorithm to utilize an outer loop (and therefore make it iterative).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Storing names and email address.
    By arya6000 in forum C Programming
    Replies: 15
    Last Post: 11-19-2008, 03:47 AM
  3. Spam Filters. ISP based email is dying.
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-05-2008, 12:05 PM
  4. email in dos
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 01-13-2002, 12:10 PM
  5. Validating email addresses
    By RetroGamer1991 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-30-2001, 11:33 PM