I’m making a console calculator, and I want to remove any whitespace that the user might enter while using the program. This is the code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
std::cout<<"Enter sum): ";
cin>>str;
str.erase(std::remove_if(str.begin(), str.end(), (int(*)(int))isspace), str.end());
cout<<str;
system("pause");
return 0;
}
if i entered 2 + 2 =, output should be 2+2=
but the output is: 2
am i using the wrong function here?
Your use of
remove_ifanderaseis fine. Your method for getting input is not.operator>>is whitespace delimited. Usegetlineinstead.