0

I've got a problem with my function that's supposed to generate a vector of random strings. However, all I get is the first element in the vector is random and the rest is empty. Here's the code:

void losuj(vector <string> &vec, int ile){
    int i, j;
    string str;
    stringstream ss;
    char c;
    for(i=0; i<ile; i++){
        for(j=0; j<5; j++){
            c= rand()%25 + 97;
            ss << c;
        }
        ss >> str;
        vec.push_back(str);
        ss.str().clear();
        str.clear();
    }
}

Any thoughts on what could be wrong?

8
  • doesn't ss.str() just return a string, instead of anything else? calling clear on that wouldn't do too much, I think? Commented Jan 28, 2015 at 18:49
  • Lose the string stream. it isn't needed. a local str inside outer for-loop using str.append(c) would be sufficient. And <random> would make this much cleaner, btw. Commented Jan 28, 2015 at 18:50
  • Please use character literals, such as 'A', instead of numbers like 97. Commented Jan 28, 2015 at 18:52
  • My understanding is that there are 26 letters in the English alphabet, but you are generating up to 25 different possibilities. Am I missing something? Commented Jan 28, 2015 at 18:53
  • 2
    Solution with <random>. Commented Jan 28, 2015 at 19:02

2 Answers 2

1

First of all, as WhozCraig stated, you don't need to use a stringstream here. std::string provides everything you need.

The reason that your code didn't work is that your eof bit was set through the operator>>. Calling stringstream::clear() resets the bit and makes the stream usable again.

Here is your code with slight modifications, working alright. I also added the fixes suggested by Thomas Matthews:

void losuj(vector <string> &vec, int ile)
{
    int i, j;
    string str;
    char c;
    for(i=0; i<ile; i++)
    {
        for (j=0; j<5; j++)
        {
            c = rand() % 26 + 'A';
            str += c;
        }
        vec.push_back(str);
        str.clear();
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Works like a charm, thanks a lot. Any idea why my version didn't work?
I added some info and a link to the answer for clarification.
don't forget about srand(time(NULL)) to seed your generator
Thanks again. I found that ss.str().clear(); would clear the stringstream in a different question on stackoverflow. @user3613500 I've got that in main, no worries.
@eilchner fyi, it would have worked had the stream been inside the first for-loop. And the string str in your case would be needless if you really wanted to use streams. Just an ostringstream ss inside the first loop, then pushing ss.str() after the inner loop completes would also have accomplished what you sought.
0

You have to reset the state of the stringstream because you've reached eof:

ss.clear();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.