469,278 Members | 2,368 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,278 developers. It's quick & easy.

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

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
4 4229
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
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
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
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.

Similar topics

4 posts views Thread by David Johnstone | last post: by
3 posts views Thread by Mike Chirico | last post: by
19 posts views Thread by Kai-Uwe Bux | last post: by
4 posts views Thread by Vijai Kalyan | last post: by
6 posts views Thread by anongroupaccount | last post: by
12 posts views Thread by Robert J. Hansen | last post: by
3 posts views Thread by Rune Allnor | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.