-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.

Community
  • 1
  • 1
Griffen
  • 397
  • 1
  • 3
  • 22
  • 4
    Step through with a debugger and you should see your real problem. – chris Aug 14 '16 at 18:32
  • @DOUGLASO.MOEN, I am not quite sure how to do this. What is meant by reference instead of value? – Griffen Aug 14 '16 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. – Barry Aug 14 '16 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`. – Pete Becker Aug 14 '16 at 18:37
  • @PeteBecker & @Barry Thank you for this suggestion. It has helped me to learn about the use of `cin` – Griffen Aug 14 '16 at 18:44
  • 1
    @user2491647 Next time try to produce a [mcve] (as you should) and you might even figure out the bug by yourself. – juanchopanza Aug 14 '16 at 18:53

1 Answers1

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.

mike.dld
  • 2,929
  • 20
  • 21
  • 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! – Griffen Aug 14 '16 at 18:41
  • @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. – marco.m Aug 16 '16 at 09:40