Why isn't this working?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 4
Reputation: helpme123456789 is an unknown quantity at this point 
Solved Threads: 0
helpme123456789 helpme123456789 is offline Offline
Newbie Poster

Why isn't this working?

 
0
  #1
Sep 13th, 2009
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.


  1. // reading a text file
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int spc_email_isvalid(const char *address) {
  8. int count = 0;
  9. const char *c, *domain;
  10. static char *specialchars = "()<>@,;:\\\"[]";
  11.  
  12. // validate the name portion (name@domain)
  13. for (c = address; *c; c++) {
  14. if (*c == '\"' &amp;&amp; (c == address || *(c - 1) == '.' || *(c - 1) ==
  15. '\"')) {
  16. while (*++c) {
  17. if (*c == '\"') break;
  18. if (*c == '\\' &amp;&amp; (*++c == ' ')) continue;
  19. if (*c < ' ' || *c >= 127) return 0;
  20. }
  21. if (!*c++) return 0;
  22. if (*c == '@') break;
  23. if (*c != '.') return 0;
  24. continue;
  25. }
  26. if (*c == '@') break;
  27. if (*c <= ' ' || *c >= 127) return 0;
  28. if (strchr(specialchars, *c)) return 0;
  29. }
  30. if (c == address || *(c - 1) == '.') return 0;
  31.  
  32. // validate the domain portion (name@domain)
  33. if (!*(domain = ++c)) return 0;
  34. do {
  35. if (*c == '.') {
  36. if (c == domain || *(c - 1) == '.') return 0;
  37. count++;
  38. }
  39. if (*c <= ' ' || *c &>= 127) return 0;
  40. if (strchr(specialchars, *c)) return 0;
  41. } while (*++c);
  42.  
  43. return (count >= 1);
  44. }
  45.  
  46. */
Last edited by John A; Sep 14th, 2009 at 3:22 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Why isn't this working?

 
0
  #2
Sep 13th, 2009
Code tags:


[code]
// paste code here
[/code]


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...

  1. char* address = "joe_smith@yahoo.com";
  2.  
  3. for (int i = 0; i < strlen (address); i++)
  4. {
  5. char letter = address[i];
  6. cout << letter;
  7. }
  8.  
  9. for (char* c = address; *c != 0; c++)
  10. {
  11. char letter = *c;
  12. cout << letter;
  13. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 255
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why isn't this working?

 
0
  #3
Sep 13th, 2009
  1. for (int i = 0; i < strlen (address); i++)
Don't use strlen in a loop condition.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Why isn't this working?

 
0
  #4
Sep 13th, 2009
Originally Posted by Dave Sinkula View Post
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: helpme123456789 is an unknown quantity at this point 
Solved Threads: 0
helpme123456789 helpme123456789 is offline Offline
Newbie Poster

Re: Why isn't this working?

 
0
  #5
Sep 14th, 2009
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:

*! “ ” # $ % & ‘ ’ ( ) * + ; , / : < > [ ] { } = ? ^ \ | ~ `
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Why isn't this working?

 
0
  #6
Sep 14th, 2009
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").
Last edited by VernonDozier; Sep 14th, 2009 at 1:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 255
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why isn't this working?

 
0
  #7
Sep 14th, 2009
Functions like strcspn or strspn might also come in handy.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,466
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Why isn't this working?

 
0
  #8
Sep 14th, 2009
Originally Posted by Dave Sinkula View Post
  1. for (int i = 0; i < strlen (address); i++)
Don't use strlen in a loop condition.
I thought most compiler would optimize it. I'll check to see
if it does with Visual Studio Express 2008.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 255
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why isn't this working?

 
0
  #9
Sep 14th, 2009
Originally Posted by firstPerson View Post
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Why isn't this working?

 
1
  #10
Sep 14th, 2009
Shouldn't the OP be looking for a C++ solution, rather than messing with archaic C-style functions and char arrays?
Reply With Quote Quick reply to this message  
Reply

Tags
email, validation, validator, verify

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1538 | Replies: 12
Thread Tools Search this Thread



Tag cloud for email, validation, validator, verify
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC