Solved

What is wrong with this function?

Posted on 2004-07-31
5
177 Views
Last Modified: 2010-04-01
I have the following function.  It executes the 'else' clause even when the 'if' clause is executed.  I cannot figure out what is going on (I'm assuming I'm missing something really easy, but I can't see it).  

      //make the e-mail lower case and remove invalid chars
      string::iterator i;
      string::iterator ib = email->begin();
      string::iterator ie = email->end();

      if(email->length() > 0)
      {
            for ( i = ib; i != ie; i++) {
                  char c = *i;
                  if ( !isalnum(c) && '_' != c && '@' != c && '.' != c) {
                        i = email->erase(i);
                        i--;
                  }
                  else
                  {
                        *i = tolower(c);
                  }
            }
      }

As another question: Since the else clause is always executed, this will cause an infinited low if there are two invalid characters passed in for the email string (e.g. "!!").  Erase will move the string::iterator forward to the next character, and then *i = tolower(c) will keep assigning this character to the previous invalid character that is stored in c.  So, if you are at the end of the string, it should loop forever, becuase you keep assigned the end of the string to tolower(c).  Is it valid to assign the end of a string to a character like that?  I wouldn't think it would be.

If it is, does it just append this to the end of the string?

Thanks ahead of time.
0
Comment
Question by:jjacksn
  • 3
5 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 total points
ID: 11684334
I think what you need to do is alter the for loop

for ( i = ib; i != email->end(); i++)

since

i = email->erase(i);

will invalidate the iterator ie if this is executed.  Never store the value of an iterator if it could be later changed.

Try that.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 11684375
Overall this works fine for me:

      std::string* email = new string("D%%avId_J&^|\@mYWebSi*t&^e#!#$%*().co!$%m");

      string::iterator i;
      string::iterator ib = email->begin();
      string::iterator ie;
      

      if(email->length() > 0)
      {
            for ( i = ib; i != email->end(); i++) {
                  char c = *i;
                  if ( !isalnum(c) &&  (c != '_') &&  (c != '@') && (c != '.') ) {
                        i = email->erase(i);
                        i--;
                  }
                  else
                  {
                        *i = tolower(c);
                  }
            }
      }
      cout << *email;

>> As another question: Since the else clause is always executed...

I don't get the else executed always, only if the if fails.

HTH
0
 
LVL 86

Expert Comment

by:jkr
ID: 11686475
mrwad99

>>i = email->erase(i);
>>will invalidate the iterator ie if this is executed

That's why the return value of 'erase()' is used. That one *is* valid.
0
 
LVL 5

Author Comment

by:jjacksn
ID: 11686505
the always executing problem is something bad with my debugger/source code i think, I adding in Message Boxes and they aren't getting displayed.  As to the change to the for loop, that worked.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 11687357
jkr,

Yeah that is correct, the return value of erase is being assigned to i, but when the for loop in the original code is then re-executed, the test will still compare i to ie.  At this point ie is of course invalid.

Please correct me if needed; as I have always said - your record talks for itself...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now