Solved

What is wrong with this function?

Posted on 2004-07-31
5
181 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 119
Why  my code (program) build with old compiler? 11 79
C++ help/ Toy problem 19 31
I could not set window to top 4 22
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 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