473,322 Members | 1,352 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,322 software developers and data experts.

why there is an error when using stringstream?

Hi,

The following code would output "error". I don't understand why there
is such error. Could you please help me?

Thanks,
Peng

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

int main() {
double e, e1;
e = 10;
std::ostringstream oss;
oss << e;

std::istringstream iss(oss.str());
iss >e1;

e = 0;
oss.str("");
oss << e;

iss.str(oss.str());
if(!(iss >e1))
std::cout << "error" << std::endl;
}

Nov 14 '07 #1
3 1841
Pe*******@gmail.com a écrit :
Hi,

The following code would output "error". I don't understand why there
is such error. Could you please help me?

Thanks,
Peng

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

int main() {
double e, e1;
e = 10;
std::ostringstream oss;
oss << e;

std::istringstream iss(oss.str());
iss >e1;
Here eofbit is set in iss.
>
e = 0;
oss.str("");
oss << e;

iss.str(oss.str());
The following fail because you didn't clear iss state thus eofbit is
still set and extraction fails.

insert:
iss.clear();
if(!(iss >e1))
std::cout << "error" << std::endl;
}
Michael
Nov 14 '07 #2
On Nov 14, 4:30 am, Michael DOUBEZ <michael.dou...@free.frwrote:
PengYu...@gmail.com a écrit :
Hi,
The following code would output "error". I don't understand why there
is such error. Could you please help me?
Thanks,
Peng
#include <string>
#include <sstream>
#include <iostream>
int main() {
double e, e1;
e = 10;
std::ostringstream oss;
oss << e;
std::istringstream iss(oss.str());
iss >e1;

Here eofbit is set in iss.
e = 0;
oss.str("");
oss << e;
iss.str(oss.str());

The following fail because you didn't clear iss state thus eofbit is
still set and extraction fails.

insert:
iss.clear();
if(!(iss >e1))
std::cout << "error" << std::endl;
}

Michael
Do I need to clear for oss?

Thanks,
Peng

Nov 14 '07 #3
Pe*******@gmail.com a écrit :
On Nov 14, 4:30 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>PengYu...@gmail.com a écrit :
>>The following code would output "error". I don't understand why there
is such error. Could you please help me?
int main() {
double e, e1;
e = 10;
std::ostringstream oss;
oss << e;
std::istringstream iss(oss.str());
iss >e1;
Here eofbit is set in iss.
>> e = 0;
oss.str("");
oss << e;
iss.str(oss.str());
The following fail because you didn't clear iss state thus eofbit is
still set and extraction fails.

insert:
iss.clear();
>> if(!(iss >e1))
std::cout << "error" << std::endl;
}

Do I need to clear for oss?
No.

I cannot remember when a failbit of badbit would be set on a ostream. It
would happen if the underlying streambuf failed and would be in an
unpredicable state so you'd rather recreate it.

Michael
Nov 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ;...
5
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears...
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...
5
by: stefan.oedenkoven | last post by:
Hi ng, i would like to exchange some data between two threads... It would be comfortable to let Thread A push some data via putc(aStream) and let Thread B listen to aStream. Is it possible...
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
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
11
by: solarin | last post by:
Hi, I've the following code: //all variables are defined in the code Class CMessage{ string CMessage::GenerateString() {
9
by: martinezfive | last post by:
Hi, I feel no doubt some documentation contains my answer, so bare with me. Given the following: #inclde <stdio.h> #include <sstream> void f() { std::stringstream a("Hello World!\n");
4
by: clb | last post by:
I am trying to use stringstreams and my book doesn't cover the included methods. For example, if I init a istringstream on a string and suck all the data out, then put more stuff into the string,...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.