int main()
{
string line;
int k = 1;
cout << "===============case1================" << endl;;
while (getline(cin, line)) //可与读到包含空格, ;等字符;但是在ss>>x时被截断
{
int sum = 0, x;
stringstream ss(line);
while (ss >> x)
{
sum += x;
}
cout << "the sum is :" << sum << endl;
++k;
cout << endl;
cout << "===============case" << k << "================" << endl;;
}
return 0;
}
Result:
===============case1================
1 2 3 4 5
the sum is :15
===============case2================
1,2,3,4,5
the sum is :1
===============case3================
1 a 2 b 3
the sum is :1
===============case4================
a 1 1 1 1
the sum is :0
===============case5================