stringstream question

D

DaLoverhino

I'm trying to get use to using stringstream and I've run across a case
that puzzles me. Perhaps you have some suggestion for the following:

Why in the code below for case 2, intVal is always 1?

// StringToInt.cc
//
//

#include <sstream>
#include <string>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::stringstream;

int main( int argc, char *argv[]) {
vector< std::string > v;
int i0,i1,i2;

v.push_back( "1");
v.push_back( "2");
v.push_back( "3");

{
// This works.
stringstream ss;

ss << v[0] << " ";
ss << v[1] << " ";
ss << v[2] << " ";

ss >> i0 >> i1 >> i2;

cout << i0 << ", " << i1 << ", " << i2 << endl;
}


{
cout << "Case 2" << endl << endl;

stringstream ss;
bool needsComma = false;
int intVal = 0;
for( vector< string >::const_iterator itr = v.begin(); itr !=
v.end(); ++itr) {
ss << *itr;
ss >> intVal;
cout << intVal << endl;
ss.flush();
}
}

return 0;
}
 
D

DaLoverhino

I'm trying to get use to using stringstream and I've run across a case
that puzzles me. Perhaps you have some suggestion for the following:

Why in the code below for case 2, intVal is always 1?

// StringToInt.cc
//
//

#include <sstream>
#include <string>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::stringstream;

int main( int argc, char *argv[]) {
vector< std::string > v;
int i0,i1,i2;

v.push_back( "1");
v.push_back( "2");
v.push_back( "3");

{
// This works.
stringstream ss;

ss << v[0] << " ";
ss << v[1] << " ";
ss << v[2] << " ";

ss >> i0 >> i1 >> i2;

cout << i0 << ", " << i1 << ", " << i2 << endl;
}

{
cout << "Case 2" << endl << endl;

stringstream ss;
bool needsComma = false;
int intVal = 0;
for( vector< string >::const_iterator itr = v.begin(); itr !=
v.end(); ++itr) {
ss << *itr;
ss >> intVal;
cout << intVal << endl;
ss.flush();
}
}

return 0;

}

I forgot to mention:

ss.str(*itr);

instead of:

ss < *itr;

Does not work either.

I am using g++.
 
D

DaLoverhino

I'm trying to get use to using stringstream and I've run across a case
that puzzles me. Perhaps you have some suggestion for the following:

Why in the code below for case 2, intVal is always 1?

// StringToInt.cc
//
//

#include <sstream>
#include <string>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::stringstream;

int main( int argc, char *argv[]) {
vector< std::string > v;
int i0,i1,i2;

v.push_back( "1");
v.push_back( "2");
v.push_back( "3");

{
// This works.
stringstream ss;

ss << v[0] << " ";
ss << v[1] << " ";
ss << v[2] << " ";

ss >> i0 >> i1 >> i2;

cout << i0 << ", " << i1 << ", " << i2 << endl;
}

{
cout << "Case 2" << endl << endl;

stringstream ss;
bool needsComma = false;
int intVal = 0;
for( vector< string >::const_iterator itr = v.begin(); itr !=
v.end(); ++itr) {
ss << *itr;
ss >> intVal;
cout << intVal << endl;
ss.flush();
}
}

return 0;

}

I was fishing around for a solution and I found out that what I have
to do is ss.clear() for reuse. ss might have flag set like eof which
causes subsequent reuse of the stream to "misbehave."

sigh... Sorry to bother you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top