I've been trying to figure what I'm doing wrong all day. I'm becoming discouraged. Can anyone help me out?

void stripSpace(string &str) {   
 int i,j,len=str.length();   
 for (i=0;j=0;i<len;i++;j++) 
    if (str[j]==' ') {      
      str.erase(j,1);      
      j--;     
    }
  str[j]='\0';
}

I just won't seem to stop until I get this fixed!!!! Any help would be appreciated. It compiles alright, but when I run the main program the string is not cout'd.

Thanks in advanced, Lamabot

Funny it even compiled. How in the world is the compiler supposed to know which statement is the evaluation statement, and which are assignment statements? Try changing the loop (eg. while() loop), and then it will work properly.

By the way, placing an else() statement after the if() and then incrementing j in there would be far more efficient than deincrementing it inside the if() statement. (This would also solve your for() loop problem by only having one loop variable to deal with).

Funny it even compiled. How in the world is the compiler supposed to know which statement is the evaluation statement, and which are assignment statements? Try changing the loop (eg. while() loop), and then it will work properly.

By the way, placing an else() statement after the if() and then incrementing j in there would be far more efficient than deincrementing it inside the if() statement. (This would also solve your for() loop problem by only having one loop variable to deal with).

I believe that is valid enhanced for loop syntax...? Anyway, I tried the while loop and it, of course, worked. :lol:

Look at this:

void stripSpace(string &str) {     
         int i;  
         for (i=0;i<str.length;i++)     
              if (str[j]==' ')        
                  str.erase(j,1);      
       }

It works for me! So I guess.... It'll suffice. I can't believe I didn't realize sooner that every loop it checks the string length which gets smaller after every erase. Joe, I think I need a break from plumbing. :-| :cheesy:

Thanks much, LamaBot

I guess I did post a similar code in another thread.... ;)

commented: Caught my little slip... ;) +8

>I guess I did post a similar code in another thread....
Well, except that you used commas to separate the multiple assignments, which will actually work.

Because that is the way multiple assignments ought to be done in C or as a matter of C++. The syntax dictates it. Try doing multiple assignments like Lazaro's on a decent compiler and you end up getting your share of errors.

...
It works for me! So I guess.... It'll suffice. I can't believe I didn't realize sooner that every loop it checks the string length which gets smaller after every erase. Joe, I think I need a break from plumbing. :-| :cheesy:

Thanks much, LamaBot

I'm quoting myself because didn't write all the code. Just noticed that. Here is the code:

#include <iostream>
#include <string>

using namespace std;
void stripSpace(string &);
int main() {
    string test("HKLM\      Software\Microsfot\  Windows\Current Version\Run");
    stripSpace(test);
    cout << test << endl;
    return 0;
}
void stripSpace(string &str) {
    for (int i=0;i<str.length();i++) 
       if (str[i]==' ') {
         str.erase(i,1);
         i--;
        }
}

Thanks for the help guys, LamaBot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.