| | |
Why isn't this working?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
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.
Thank you.
C++ Syntax (Toggle Plain Text)
// 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 == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) { while (*++c) { if (*c == '\"') break; if (*c == '\\' && (*++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); } */
Last edited by John A; Sep 14th, 2009 at 3:22 am. Reason: added code tags
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
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...
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.
[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...
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < strlen (address); i++)
"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
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
Don't use strlen in a loop condition.C++ Syntax (Toggle Plain Text)
for (int i = 0; i < strlen (address); i++)
•
•
Join Date: Sep 2009
Posts: 4
Reputation:
Solved Threads: 0
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:
*! “ ” # $ % & ‘ ’ ( ) * + ; , / : < > [ ] { } = ? ^ \ | ~ `
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:
*! “ ” # $ % & ‘ ’ ( ) * + ; , / : < > [ ] { } = ? ^ \ | ~ `
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
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
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. "&"). Last edited by VernonDozier; Sep 14th, 2009 at 1:48 pm.
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
•
•
•
•
Don't use strlen in a loop condition.C++ Syntax (Toggle Plain Text)
for (int i = 0; i < strlen (address); i++)
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
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
![]() |
Similar Threads
- Flash Player stop working since Ad-aware (Web Browsers)
- Internet Explorer not working. (Web Browsers)
- News Story: Oxford University reveals remote working reduces global warming (IT Professionals' Lounge)
- Flash Player stop working and won't reinstall (Windows NT / 2000 / XP)
- messanger not working. (Web Browsers)
- cd burner not working (Storage)
- Working on new design (DaniWeb Community Feedback)
Other Threads in the C++ Forum
- Previous Thread: Best way to maintain multiple client sockets?
- Next Thread: Linked List Text file w/ class
Views: 1538 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for email, validation, validator, verify
acceptableuse action activation alerts asp asp.net authentication bounce browser budgets business buttons c++ censorship ceo chat cloud conference confidentiality daniweb dating dictionary download dvorak ecommerce election electronicdiscovery email encryption eudora firefox form gmail gmailoutage google government hacking hosting internet iphone javascript keepass lies mail malware microsoft mobilephone mozilla naked nazi news onecare online opensource opinion oracle outage outlook palamida penelope phishing phones php privacy registrationform retention rolex rss scam security send smtp socialmedia socialnetworking socialnetworks spam spammers spamming stocks string studio sync technology textbox thunderbird translation trends twitter uk.government validation validator verify video virtualization virus wave webmail words yahoo zend







