trying code for removing all white space from a line of a txt file

hi all,
I am amateur in c++ coding hope you guys will help me. this is the code I tried in my code block 10.05.....tried code for removing all white space from a line of a txt file. this is my code it runs but i cant get the result i need. can you guys fix it or provide me the full code of the program.
thank you all...

#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <ctype.h>
#include <algorithm>

using namespace std;

string removespace(string str){
for (int i = str.length()-1; i >= 0; --i) {
if(str[i] == ' ')
str.erase(i, 1);
}
return str;
}

int main (void){
string str, line;
string processingLine[5];

ifstream myfile ("crap.txt");

getline (myfile, processingLine[0]);
line = removespace(processingLine[0]);

cout << "Line #" << " : " << line << endl;

cin.get ();
return 0;

}

the input was in the file: if (zxc + 3 );
the output is the same: if (zxc + 3 );
use string::find and string::replace
i need to run this in a loop so i need this white space remove thingy as a function so i can call it as many times as i want...... :( and i dont know how to use str.find() and str.replace() function and what header file is needed.....
there's no reason you can't run it in a loop (just run loop till you are able to string::find white space)

http://www.cplusplus.com/reference/string/string/find/?kw=string%3A%3Afind
http://www.cplusplus.com/reference/string/string/replace/?kw=string%3A%3Areplace

also the header is "string" (you can find it on the left side, as the name of the menu where all functions are)
I myself would just use a stringstream, and the extraction operator if you are only trying to remove spaces from your string.

Your code works for me when I compile and run it. With crap.txt containing
if (zxc + 3 );

I get output:
Line # : if(zxc+3);

I agree with dhayden. I have the same result.
IDE tool:VS2010
It still looks scary. Example.
foo = "   a-very-long-word";

Three spaces. Three calls to erase. The a-very-long-word is copied three times.
How about:
1
2
3
4
auto ib = str.begin();
auto ie = str.end();
ie = std::remove(ib, ie, ' ');
str.resize( std::distance( ib, ie ) );
thanks for all the help guys i made a mistake on the for loop thats why i was having problem in the outputs i fixed it later.... thanks again...

I am basically making a scanner for a simple c compiler in my compiler construction course. I will be posting more problems soon. if you guys dont mind.... :P

int main (void){
string str, line[20];
string processingLine[20];
int counter=1;

ifstream myfile ("crap.txt");

for (int i = 0; i<=20 ; ++i){
getline (myfile, processingLine[i]);
line[i] = removespace(processingLine[i]);
}

for (int i = 0; i<=20 ; ++i)
{
cout << "Line #" << i << ":" << line[i] << endl;
}

cin.get ();
return 0;

}
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.