Hey,

#include<iostream>
#include<sstream>
using namespace std;

void manage(vector <string> scoreSheet)
{
int i;
stringstream q;
string z;
for(i=0;i<scoreSheet.size();i++)
{
q<<scoreSheet[i];
q>>z;
cout<<z<<endl;
}
}

I expected this program to print the first word of each string in vector but is printing the first value over and over again,any idea?

Desperate here :-/

Declare the stringstream inside the for-loop's body:

for(i = 0; i < scoreSheet.size(); i++)
{
  [B]stringstream q;[/B]
  q << scoreSheet[i];
  q >> z;
  cout << z << endl;
}

:)

Edit::
In some cases, you might want to add an additional check as well:

for(i = 0; i < scoreSheet.size(); i++)
{
  stringstream q;
  [B]if( !scoreSheet[i].empty() ) {[/B]
    q << scoreSheet[i];
    q >> z;
    cout << z << endl;
  [B]}[/B]
}

Declare the stringstream inside the for-loop's body:

for(i = 0; i < scoreSheet.size(); i++)
{
  [B]stringstream q;[/B]
  q << scoreSheet[i];
  q >> z;
  cout << z << endl;
}

:)

thank u worked :)

Wouldn't

stringstream q;
q << scoreSheet;
string x=q.str();

Do the trick?

Nope, because you just get back the whole string, not the first word.
And I think you meant: q << scoreSheet[B][i][/B]; .

Nope, because you just get back the whole string, not the first word.
And I think you meant: q << scoreSheet[B][i][/B]; .

No,not workin, the output is

#include<iostream>
#include<sstream>
#include<vector>
using namespace std;

int main()
{
 int i;
 vector <string> scoresheet;
 stringstream q;
 string z;
 scoresheet.push_back("QWEr");
 scoresheet.push_back("Q");
 scoresheet.push_back("QEr");
 scoresheet.push_back("wQqE");
 for(i=0;i<scoresheet.size();i++)
 {
  q<<scoresheet[i];               
  z=q.str();
  cout<<z<<endl;
 }
 cin.get();
}

QWErQQErwQqE

Is there any other way?
i mean to clear the buffer??


EDIT:The method posted earlier worke btw, Thank a lot to tux4life :)

Is there any other way?
i mean to clear the buffer??

This is not for clearing the buffer, but for resetting the error flags: q.clear(); (You can read more about it here)
As you mentioned in your first post: The first value is printing over and over.
This is because the stringstream goes into an eof state.

There is an alternative al-though it might be faster if you clear the buffer.

#include<iostream>
#include<sstream>
#include<vector>
using namespace std;

int main()
{
 int i;
 vector <string> scoresheet;
 //stringstream q; //Donot Declare the stringstream here.
 string z;
 scoresheet.push_back("QWEr");
 scoresheet.push_back("Q");
 scoresheet.push_back("QEr");
 scoresheet.push_back("wQqE");
 for(i=0;i<scoresheet.size();i++)
 {
 stringstream q;//Declare stringstream inside loop.
  q<<scoresheet[i];               
  z=q.str();
  cout<<z<<endl;
 }
 cin.get();
}

There is an alternative al-though it might be faster if you clear the buffer.

#include<iostream>
#include<sstream>
#include<vector>
using namespace std;

int main()
{
 int i;
 vector <string> scoresheet;
 //stringstream q; //Donot Declare the stringstream here.
 string z;
 scoresheet.push_back("QWEr");
 scoresheet.push_back("Q");
 scoresheet.push_back("QEr");
 scoresheet.push_back("wQqE");
 for(i=0;i<scoresheet.size();i++)
 {
 stringstream q;//Declare stringstream inside loop.
  q<<scoresheet[i];               
  z=q.str();
  cout<<z<<endl;
 }
 cin.get();
}

What has been said in the second post of this thread?
BTW, he only wants the first word, not the whole string, so z=q.str(); is incorrect (but it seems working if you're dealing with strings which only contain one word).

Assign (empty) zero length string.

for(i=0;i<scoresheet.size();i++)
 {
  q << scoresheet[i];
  z=q.str();
  cout<<z<<endl;
  q.str("");
 }
This article has been dead for over six months. Start a new discussion instead.