I am working on a small assignment, and part of it requires me to split the string into integers and characters. These integers and characters are then stored in separate vectors. For example, if I enter '* + / 9 8 7', I want to store the '*','+' and '/' in one vector and the 9, 8, and 7 in another vector. Below is the code I wrote for this program
string InputString;
getline(cin,InputString);
stringstream ss(InputString);
vector<int>operands;
vector<char>operators;
char op;
int num;
while(ss>>op)
{
if((op=='+')||(op=='-')||(op=='*')||(op=='/'))
{
operators.push_back(op);
}
else
{
ss>>num;
operands.push_back(num);
}
}
for (unsigned int k = 0;k<operands.size();k++)
{
cout<<operands[k]<<" ";
}
cout<<endl;
for (unsigned int x = 0;x<operators.size();x++)
{
cout<<operators[x]<<" ";
}
The problem is that my output does not store the integers correctly. My characters are stored correctly (when I print out the contents of my integer vector in my second for loop, I only get the last number entered). I hope that my question is not too confusing. I couldn't find other related posts on StackOverflow