0

Trying to take an input string and remove the spaces from it. Except when it reaches a white space it deletes everything after that as well.

Here's my code (got it off a similar topic on stackoverflow):

string removeSpaces(string s){
    s.erase(remove(s.begin(),s.end(), ' '),s.end());
    return s;
}

For instance, if I input "1 +1", it returns "1". How could I fix this?

Here's a full example of what I tried:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string input;

string removeSpaces(string s){
    s.erase(remove(s.begin(),s.end(), ' '),s.end());
    return s;
}

int main(){
    getline(cin, input);
    removeSpaces(input);
    cout << input;
}

That returns a string identical to the input with no spaces removed.

  • 1
    That code works fine for me. – Galik Nov 16 '14 at 19:49
  • You could start by trying to understand what others' code might mean, instead of copying it blindly.. :) – mlwn Nov 16 '14 at 19:49
  • 1
    How did you get `s`? `cin >> s;` might not work as you expect. Try `getline()` instead. – πάντα ῥεῖ Nov 16 '14 at 19:51
  • @mlwn, in all honesty, I have no clue how to code in C++. My professor is a complete a-hole and hasn't taught us anything all semester but expects us to code complex stuff. Just trying my best to get my assignments done before they're due. Wish I had time to really figure this stuff out and understand it. :/ – Aubrey Sapp Nov 16 '14 at 19:51
  • 2
    @AubreySapp get a good book: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Take control of your education :) – Galik Nov 16 '14 at 19:54
  • @ πάντα ῥεῖ, just tried getline(), now the output is exactly the same as the input. Space wasn't removed. – Aubrey Sapp Nov 16 '14 at 19:54
  • You need to post more code. What you posted works here: http://codepad.org/buzKwb6t – Galik Nov 16 '14 at 19:55
  • @Galik, if I had time to read a book I would totally do it(probably will after the semester). I'm in the military as well as taking classes so my time is very limited. Thanks for the link though! Will definitely pick up one of those books when I get a chance! – Aubrey Sapp Nov 16 '14 at 19:57
  • @Galik, added in more code. I took the essentials from my project only (gives me the same error), if you want me to post the full ~350 lines let me know. – Aubrey Sapp Nov 16 '14 at 20:03
  • What is `remove` is in `s.erase(remove(s.begin(),s.end(), ' '),s.end());`? – LyingOnTheSky Nov 16 '14 at 20:06
  • @LyingOnTheSky, http://www.cplusplus.com/reference/cstdio/remove/ – Aubrey Sapp Nov 16 '14 at 20:08
  • @AubreySapp It isn't; your function uses 3 parameters; the function you referenced uses 1 parameter. – LyingOnTheSky Nov 16 '14 at 20:09
  • @LyingOnTheSky, I got the code of this site with multiple people saying it worked. In all honesty I just need something that will work so I can finish this assignment before its due in a few hours. I'm beyond the point of trying to learn something right now. – Aubrey Sapp Nov 16 '14 at 20:11

1 Answers1

3

The function removeSpaces takes the input by value (it creates a copy) so it doesn't change the input string. The string with spaces removed is returned from the function so you need to use that instead. Try:

string output = removeSpaces(input);
cout << output;
Chris Drew
  • 14,926
  • 3
  • 34
  • 54