I am using istringstream to put strings separated by commas to own std::strings.
I have done this wich works:

std::string One, Two, Three, Four, Five, Six;
std::string Value = "a,b,c,d,e,f";
istringstream is(Value);

while ( getline(is, One, ',') && getline(is, Two, ',') && getline(is, Three, ',') && getline(is, Four, ',') && getline(is, Five, ',') && is >> Six)
   {
	//Action;
   }

So I am extracting Value that consists of 6 strings separated by ",".
Now if I will take "f" away from Value. This will not work. The std::strings One to Five will not be assigned any strings as it is only 5 elements in the string Value now.
Why does this happening ?
Is this possible to make it work when you dont know how many strings you are trying to exctract from Value. The maximum will be 6 though.

Not if the substrings need to go into a rigid set of variables. You could declare an array/vector that could hold up to 6 strings and extract them out of the original string one at a time storing them in the array as you go. In the array they won't be assigned to string variables by the name of One, Two, etc, however.

Okay, that sounds nice. I know exactly how vectors works and how to assign the elements but what I dont really know how to do, is to exctract each string separated by ',' in my std::string Value.
I know how to do if I know how many strings that are separated but not if I dont know how many strings that is separated by ','
Any idéas how to do that ?
Thanks...

std::vector<string> Store(5);
std::string Value = "a,b,c,d,e,f"; //Strings can differ. This example is 6, 
                                   //sometimes it can be 5,4,3,2 or 1

Not if the substrings need to go into a rigid set of variables. You could declare an array/vector that could hold up to 6 strings and extract them out of the original string one at a time storing them in the array as you go. In the array they won't be assigned to string variables by the name of One, Two, etc, however.

This is practially pretty much what Lerner said ...

std::string Value = "a,b,c,d,e,f";
istringstream is(Value);
std::string s;
std::vector<string> v;

while (getline(is, s, ','))
{
    // add item
    v.push_back(s);
}
// iterate through the vector and output each item ...
for(std::vector<string>::iterator it = v.begin(); it != v.end(); it ++)
{
    cout << *it << endl;
}

Thank you very much mitrmkar and Lerner. That did solve it. I was not really sure how to continuosly get the next string after each ',' but now I know how to do that.
Thanks... :)

This is practially pretty much what Lerner said ...

std::string Value = "a,b,c,d,e,f";
istringstream is(Value);
std::string s;
std::vector<string> v;

while (getline(is, s, ','))
{
    // add item
    v.push_back(s);
}
// iterate through the vector and output each item ...
for(std::vector<string>::iterator it = v.begin(); it != v.end(); it ++)
{
    cout << *it << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.