I suppose the "proper result" that you wish to get is an array (or a linear structure) of strings that contains the individual elements in the input separated by white spaces?
This process is referred to
string tokenization. In languages in Java, there are built-in tokenizers to do the job. In C++, there is also a cleaner way to do this.
(There are many subtleties when implementing tokenization -- essentially one has to exhaust the situations in which something goes wrong in the input.)
We would use the libraries
String,
stringstream, STL algorithms along with
iterators, and suppose we use
vector as the container:
1 2 3 4 5
|
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
|
1 2 3 4 5 6 7 8 9
|
using namespace std;
string input("111 222 333 444 555 666");
istringstream isstr(input);
vector<string> tokens;
copy( istream_iterator<string>(isstr),
istream_iterator<string>(),
back_inserter< vector<string> >(tokens) );
|
The
back_inserter is to perform overwriting in preference to insertion at the end of the vector.
If you are just printing out the tokens without storing them, the third argument of copy() can be replaced by the iterator directly to the output device (
std::cout):
ostream_iterator<string>(cout, "\n")
.
A similar method applies if you want to convert the tokens further into
ints, or other types.