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.