473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 4465
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Johnstone | last post by:
Hi Group, I am using gcc 3.2.2 on linux 2.4.19-4GB (SuSe 8.2). I have a situation where I need to tie an ostringstream to an istringstream so I can write to one and read back what I wrote...
3
by: Mike Chirico | last post by:
Sorry about the newbie question. What's the best way to convert numbers to strings in C++. The following works, but is it better than using the sprintf() "old C" way of converting? #include...
19
by: Kai-Uwe Bux | last post by:
Hi folks, I have trouble writing a class, derving from stringstream, that collects item and once it's done will write them to std::cout in one go. It works fine except when I use it as a...
4
by: Vijai Kalyan | last post by:
I wrote the following function as a curiosity: template<typename SourceType, typename DestinationType> DestinationType NumericCast(const SourceType& value) { std::wstringstream strbuf; strbuf...
6
by: anongroupaccount | last post by:
I have a base class that takes a string to its constructor. I want to construct a concrete class that does this: class concrete: public base { public: concrete() : base( /* create a...
12
by: Robert J. Hansen | last post by:
While writing some code to demonstrate different local search strategies, I found something kind of unusual. I suspect it's a bug in my understanding rather than a bug in GCC, and I'm hoping...
9
by: Àî°× | last post by:
hi all: I want erase a stringstream' content for input new data to it. example: std::stringstream stm; stm<<"this is a string"; std::cout<<stm.str(); // here print:this is a string
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
3
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.