By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

stringstream used to convert int to string does work only once?

P: n/a
Hi,

the following snippet shows once executed this output:
2
2

I'd have rather expected this output:
2
10

Does anyone know why?

thx4hints,

FF

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

int main(void)
{
string apName;
stringstream ss;

ss << 2;
ss >> apName;
cout << apName << endl;
ss << "10";
ss >> apName;
cout << apName << endl;
return 0;
}
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Field wrote:
the following snippet shows once executed this output:
2
2

I'd have rather expected this output:
2
10

Does anyone know why?
Sure. See below.

thx4hints,

FF

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

int main(void)
{
string apName;
stringstream ss;

ss << 2;
ss >> apName;
cout << apName << endl;
ss << "10";
ss >> apName;
cout << apName << endl;
return 0;
}


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

int main(void)
{
string apName;
stringstream ss;

ss << 2;
ss >> apName;
cout << apName << endl;
ss << "10";
if (ss >> apName)
cout << apName << endl;
else
cout << "Could not convert\n";
return 0;
}
V
Jul 23 '05 #2

P: n/a
Field wrote:
Hi,

the following snippet shows once executed this output:
2
2

I'd have rather expected this output:
2
10

Does anyone know why?

I don't believe the problem is related to converting an int to a string. It
seems to be related to the state of stringstream after the first input and
output. I'm not sure exactly what state it's put into, but if the state is
reset, the second sequence of calls will work as expected. I'll play
around a bit more to see if I can't find a way to determine the exact state
the stringstream is in after the first sequence of calls.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #3

P: n/a
Thing is, you need to clear the state using clear():

ss.clear(stringstream::goodbit);

before the next call.

Found solution on this site:
http://www.programmingforums.org/for...ead.php?t=4224

Thanks to everyone bothering.
Needed to ask here to find the answer elsewhere. As usual.. :)

Little fix: instead of
ss << "10";
I meant:
ss << 10;

obviously.. ;)

kr,

FF

On Fri, 17 Jun 2005 02:29:05 +0200, Field <cl******@think-future.com>
wrote:
Hi,

the following snippet shows once executed this output:
2
2

I'd have rather expected this output:
2
10

Does anyone know why?

thx4hints,

FF

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

int main(void)
{
string apName;
stringstream ss;

ss << 2;
ss >> apName;
cout << apName << endl;
ss << "10";
ss >> apName;
cout << apName << endl;
return 0;
}


Jul 23 '05 #4

P: n/a
Field wrote:
Thing is, you need to clear the state using clear():

ss.clear(stringstream::goodbit);

before the next call.

Found solution on this site:
http://www.programmingforums.org/for...ead.php?t=4224

Thanks to everyone bothering.
Needed to ask here to find the answer elsewhere. As usual.. :)

Little fix: instead of
ss << "10";
I meant:
ss << 10;

obviously.. ;)

kr,

FF

On Fri, 17 Jun 2005 02:29:05 +0200, Field <cl******@think-future.com>
wrote:


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

int main(void)
{
string apName;
stringstream ss;
ss << 2;
ss >> apName;
cout << apName << endl;
ss.clear();
ss << "10";
ss >> apName;
cout << apName << endl;
return 0;

}

This is what I had done before replying. You didn't ask how to make it
work. You asked why it behaves as it does.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.