This is what I have so far. This isn't working and this isn't even close to working. I'm not that experienced at C++ so I was hoping that someone would help me please. The assignment is basically to write a code to validate an email address to see what characters are allowed and what characters aren't allowed. Please help in anyway possible.

Thank you.

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int spc_email_isvalid(const char *address) {
  int        count = 0;
  const char *c, *domain;
  static char *specialchars = "()<>@,;:\\\"[]";

  // validate the name portion (name@domain)
  for (c = address;  *c;  c++) {
    if (*c == '\"' &amp;&amp; (c == address || *(c - 1) == '.' || *(c - 1) ==
        '\"')) {
      while (*++c) {
        if (*c == '\"') break;
        if (*c == '\\' &amp;&amp; (*++c == ' ')) continue;
        if (*c < ' ' || *c >= 127) return 0;
      }
      if (!*c++) return 0;
      if (*c == '@') break;
      if (*c != '.') return 0;
      continue;
    }
    if (*c == '@') break;
    if (*c <= ' ' || *c >= 127) return 0;
    if (strchr(specialchars, *c)) return 0;
  }
  if (c == address || *(c - 1) == '.') return 0;

  // validate the domain portion (name@domain)
  if (!*(domain = ++c)) return 0;
  do {
    if (*c == '.') {
      if (c == domain || *(c - 1) == '.') return 0;
      count++;
    }
    if (*c <= ' ' || *c &>= 127) return 0;
    if (strchr(specialchars, *c)) return 0;
  } while (*++c);

  return (count >= 1);
}

*/

Edited 7 Years Ago by John A: added code tags

You can use pointers and pointer arithmetic to isolate the characters in a string, but I think the code is much more readable if you use the [] operator...

char* address = "joe_smith@yahoo.com";

    for (int i = 0; i < strlen (address); i++)
    {
        char letter = address[i];
        cout << letter;
    }

    for (char* c = address; *c != 0; c++)
    {
        char letter = *c;
        cout << letter;
    }

Again, both work, but I think the first is slightly more readable than the second. As to your program, the code is a bit hard to follow. One, if this is a boolean function, since this is C++, not C, might as well take advantage of that and make use words like bool, true, and false so it's obvious that that is what you are doing.

First, decide what's legal and what isn't in an e-mail name. the explain in a few sentences in English what you are doing. I can sort of tell, but not really. What input works? What doesn't? What's breaking here? What valid e-mail address is being returned as invalid? What invalid e-mail address is being returned as valid? Post with code tags or it's really hard to read the code.

Edited 3 Years Ago by happygeek: fixed formatting

for (int i = 0; i < strlen (address); i++)

Don't use strlen in a loop condition.

Good point. I was thinking in terms of code readability rather than efficiency, but there's no reason you can't have both. E-mails will likely be fairly short strings, but you're correct; calculations like these should happen before the loop starts since they won't change and you're just going to keep recalculating the same thing each trip through the loop.

Thanks for replying. What I am trying to do is the following:

Email Address,First Name,Last Name,Custom Data

* [Email Address] Allows up to 255 characters
* [First Name] Allows up to 50 characters
* [Last Name] Allows up to 50 characters
* [Custom Value] Allows up to 255 characters

Allowed Characters:

* Letters (A-Z) and (a-z)
* Hypens (-)
* Underscores (_)
* Numbers (0-9)
* Decimal or period (.)
* Only one single apostrophe allowed (')

Characters Not Allowed:

*! “ ” # $ % & ‘ ’ ( ) * + ; , / : < > [ ] { } = ? ^ \ | ~ `

Do it one character at a time. Isolate the character, then test it. You have a whole bunch of compound statements which, while they may correct, make the code a little hard to follow, at least for me. The last name, first name, and custom value validation should have their own criteria and be in their own functions. Stick with the e-mail validation.

I'm not 100% sure what the e-mail validation rules are. Double-check them. I think you need a rule in there where you need to have exactly one @. Could be wrong.

Anyway, nail down some very exact criteria (you might have to research this), then go through character by character and test them. Any illegal character returns false. Anything that passes all tests returns true. Check the cctype library. It has some good character testing functions like isalnum , which could come in handy. you may want to repost the code, as it got a little garbled (i.e. "&amp").

Edited 7 Years Ago by VernonDozier: n/a

I thought most compiler would optimize it. I'll check to see
if it does with Visual Studio Express 2008.

Contained in the link I posted was another link to a discussion thread that mentions some things that would make this "assumed optimization" questionable (for the particular case being discussed). Perhaps that can shed further light on this for you.

Yes, that's what I need, but I'm really confused about how I got to where I'm at and getting tense. This is an important assignment and I might need to start over. Simpler coding is of course recommended for this assignment, but could you get me started on how you think this project should be?

Yeah, you can't have two @'s in a row. That is something I missed, thank you. But could you help me get started on this. I'm extremely new at this and very amateur when it comes to programming, most of this I got looking at the book. Do you think I should start over? Is it too much to ask of you to ask you if you could please write how you would do this program for atleast one example of true and false and maybe then I could pick it up.

Help is much appreciated,
Thank you.

One approach could be this. Again, I think you need to post the precise criteria before you start. For example, can you really have an apostrophe in an e-mail address? Could you have all underscores (i.e. _____@_____) ? All periods? ...........&.......... ? Can the ampersand be the first or last character? Find out for sure, but using these criteria:

  1. Exactly one &
  2. Letters are OK
  3. Digits are OK
  4. Underscores are OK
  5. Hyphens are OK
  6. Periods are OK
  7. Everything else is illegal.

Go through character by character, isolating and testing one character at a time:

bool TestEmailAddress (string emailAddress)
{
    int emailLength; // fill in length variable
    int numAmpersands; // initialize to 0

    // set up loop
    for (int i = 0; i < emailLength; i++)
    {
         char charToTest = emailAddress[i]; // isolate a character.

         bool charIsLegal = false;
         // variety of tests to check if charToTest is acceptable.  if so,
         // set charIsLegal to true.  In addition, if it's an ampersand,
         // increment numAmpersands.

         // check whether charIsLegal is still false.  if it is, return false.
    }

    // last test.  If there was exactly one ampersand, return true.
    // otherwise, return false.
}

Remember to look at the cctype library. Some of those functions could be useful in your checks.

This article has been dead for over six months. Start a new discussion instead.