Verifty that a valid email address entered?

I'm working on a school project and it's driving me nuts. The program requires taht a user enteres their email address, name, age etc..... the part I'm having a problem with is that we have to verify the user entered @ in the email entry. if they don't enter the @ and hit submit it must display a message that a valid email is required. The only thing I can't get to work, or figure out how to work is being able to verify the user put the @ in their entry.

Can anybody help me out?

I tried to search for help, but I get things like "email addres is required when registering for this site"....I'm not really sure what to even search for to get a good stepping stone.

BTW - I'm using C++ code for the program

Thanks
Last edited on
Well, if you are looking at a string...you could iterate through it and look for an @ symbol like this:

1
2
3
4
5
6
bool checker(string input) {
     for(int a = 0; a < input.size(); a++) {
          if(input.at(a) = '@') return true;
     }
     return false;
}


Or you could make a more dynamic function that wants the symbol to check for as well:

1
2
3
4
5
6
bool checker2(string input, char what) {
     for(int a = 0; a < input.size(); a++) {
          if(input.at(a) = what) return true;
     }
     return false;
}
Last edited on
How about...
1
2
3
4
bool containsAtSign(const std::string& s)
{
   return s.find("@") != std::string::npos;
}
Last edited on
well here is part of my issue. I'm using C++ for a cgi program. so I have my data coming in from an HTML page. so when the data comes in, its not coming in as a string and I'm having problems converting it over to a string. I think both of your suggestions would work, but Now I gotta figure out how to convert the char to a string, then I could search it properly?

thanks for the help
>> I'm using C++ for a cgi program. so I have my data coming in from an html >> page. so when the data comes in, its not coming in as a string

is it coming as (char*) or what?

if so,

use this code
1
2
3
4
5
6

  char str[] = "datafrom@htmlpage";
  char * pch;
  pch=strrchr(str,'@');
  return (*pch)?true:false;   



or use javascript code to check if it is valid email or not, at client site
usually everyone uses javascript for that kinda checks



Last edited on
SteakRider
thank you very much, I'm at work but that looks like it will do exactly what I need it to do, thank you for that post. I will try it tonight when I get home and post my results but it looks like it should do the trick
Just for info: std::string has an implicit conversion constructor taking a const char*. Thus the following is valid:
1
2
3
4
5
6
7
8
9
10
bool containsAtSign(const std::string& s)
{
   return s.find("@") != std::string::npos;
}

int main()
{
   const char* foo = "some_text@fish.com";
   containsAtSign(foo);
}


Anyway, SteakRider has given you a direct solution so this is just an aside really.
Last edited on
Topic archived. No new replies allowed.