[NEWBIE] istringstream-syntax

M

Markus Kern

Hi!

I want to do what has been tried before
by many: Converting a string into an integer
*the C++ way* (= using streams rather than atoi).
Knowing I'm not the first to wonder how, I
googled getting this solution only:

-----------------
int number;
string test = "1234";
istringstream buf(test);
buf >> number;
-----------------

However, what I want to do is to use buf *after*
defining test --or at least, after assing any
value to test, so something like:


-----------------
int number;
string test;
istringstream buf;

test="1234";
// and now turn "test" into an
// integer, maybe something like:
// buf(test) >> number;
// or
// buf=test; buf >> number;
------------------

Oh, BTW, I *have* read http://www.parashift.com/cpp-faq-lite/


Thanks for any suggestions in advance

M.K.
 
D

Dietmar Kuehl

Markus said:
int number;
string test;
istringstream buf;

test="1234";
// and now turn "test" into an
// integer, maybe something like:
// buf(test) >> number;
// or
// buf=test; buf >> number;

Maybe you should use something which will work:

/**/ buf.str(test); buf >> number;

Of course, you might want to use the more convenient
lexical cast from boost:

/**/ number = boost::lexical_cast<int>(test);
 
M

Markus Kern

Ah! Thank you, that was exactly what
I was looking for.

Never heard of boost:: before, however,
I googled and found boost.org where
I'm going to spend an hour or two :)

Thanks again!

M.K.
 
M

Markus Kern

Hi!

Still, the whole thing does not work the way I want it to.
Here's my code:

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

int main()
{
int number;
string test;
istringstream buf;

test="1234";
buf.str(test); buf >> number;
cout << test << " : " << number << endl;

test="555";
buf.str(test); buf >> number;
cout << test << " : " << number << endl;

return 0;
}
-------------------

Here's the output:

1234 : 1234
555 : 1234


Why is this? What can I do to get a "555 : 555" as second out?

Thanks

MK
 
K

Karl Heinz Buchegger

Markus said:
Hi!

Still, the whole thing does not work the way I want it to.
Here's my code:

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

int main()
{
int number;
string test;
istringstream buf;

test="1234";
buf.str(test); buf >> number;
cout << test << " : " << number << endl;

test="555";
buf.str(test); buf >> number;
cout << test << " : " << number << endl;

return 0;
}
-------------------

Here's the output:

1234 : 1234
555 : 1234

Why is this? What can I do to get a "555 : 555" as second out?

The problem is, that when you do the first extraction, the stream has already gone
into a fail state, since internally it reads past the 'end of file'. If you think
of it, it needs to do that in order to determine, that '4' was the last digit.
So you need to clear that stream from that fail state otherwise the stream won't
react to any other operation request. Member function clear() does this.

But: You should get into the habit of using an istringstream in such contexts
only one, and you will have no problems at all. Best encapsulate the functionality
into a function and forget about the details:

int ToValue( std::string Text )
{
istringstream buf( Text );
int number;

buf >> number;
return number;
}

int main()
{
std::cout << ToValue( "1234" ) << std::endl;
std::cout << ToValue( "555" ) << std::endl;
}

(You might want to add some error handling in the above, but in principle that's
it).
 

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

Similar Threads

istringstream Conversion Question 1
istringstream Conversion Question 5
static istringstream 9
istringstream 10
Parsing a string into integers using istringstream 2
TF-IDF 1
Tasks 1
feedback on code design 23

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top