-1

I have read these two questions already:

  1. Remove spaces from std::string in C++
  2. remove whitespace in std::string

For some reason, I can never get the solutions to work correctly. In my program, I collect input from the user and pass it to an std::string. From there, I want to remove all of the spaces in it. For example, if the user inputs "3 + 2", I would like it to change to "3+2".

What happens is, whatever is before the first string is kept. Here is my program:

#include <iostream>

std::string GetUserInput() {
    std::cout << "Please enter what you would like to calculate: ";
    std::string UserInput;
    std::cin >> UserInput;
    return UserInput;
}
int PerformCalculation(std::string Input) {
    Input.erase(std::remove_if(Input.begin(), Input.end(), ::isspace), Input.end());
    std::cout << Input;
    return 0;
}
int main() {
    std::string CalculationToBePerformed = GetUserInput();
    int Solution = PerformCalculation(CalculationToBePerformed);
    return 0;
}

So when I run this program and type in "3 + 2", the output is "3".

Here is my console:

Please enter what you would like to calculate: 3 + 2
3
Process finished with exit code 0

I cannot figure out how to resolve this. I even tried using a solution that involved using a regex to remove all the \s characters, and that gave me the same issue.

6
  • 4
    Step through with a debugger and you should see your real problem. Commented Aug 14, 2016 at 18:32
  • @DOUGLASO.MOEN, I am not quite sure how to do this. What is meant by reference instead of value? Commented Aug 14, 2016 at 18:35
  • 6
    As a general guideline - if the output of your function isn't what you expect, a good place to start is if the input to your function is what you expect. Commented Aug 14, 2016 at 18:37
  • 2
    @user2491647 - passing by reference won't affect this, so don't worry about it. The problem is with the input, not how it's handled. Look at the value of UserInput after the extraction from cin. Commented Aug 14, 2016 at 18:37
  • 1
    @user2491647 Next time try to produce a minimal reproducible example (as you should) and you might even figure out the bug by yourself. Commented Aug 14, 2016 at 18:53

1 Answer 1

2

To read the complete line (up to terminating \n), you need to use e.g. std::getline(std::cin, UserInput);. Otherwise, you're currently reading text up to first whitespace character.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for a direct answer. As someone who has just started learning programming in C++ by following an online tutorial, this answer helps me a lot more than a vague answer that doesn't explain the problem!
@user2491647 the "vague answers" you mention are actually attempts to make you think, instead of just giving you the direct answer, that will be useless for your next question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.