#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
usingnamespace std;
string noBlanks( string str )
{
string result;
for ( char c : str ) if ( !isspace( c ) ) result += c;
return result;
}
int main()
{
int num;
int sum = 0;
string line;
cout << "Input an expression: ";
getline( cin, line );
line = noBlanks( line );
stringstream ss( line );
while ( ss >> num ) sum += num;
cout << "Result is " << sum << '\n';
}
string noBlanks( string str )
{
string result; // create an empty string
for ( char c : str ) // for every char in the input string named "str"...
{
if ( !isspace( c ) ) // if that char is not a space...
{
result += c; // put a copy of that char on the end of the string named "result"
}
}
return result;
}
This function copies every character in the input string to a new string, EXCEPT the spaces, and then returns that new string.