473,809 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stream extraction failing

Hello all,

#include <sstream>
#include <iostream>

int main()
{
std::istringstr eam iss("6.5e");
double size;
char fit;
iss >size >fit;
std::cout << "size(" << size << "), fit(" << fit << ")" << std::endl;
}

This program doesn't extract the elements okay (apparent garbage
output) but if the input stream is 6.5d it does. I figure this is
because the extraction operator thinks it's going to have a number
like 6.5e+12??? Is there a way I can turn this feature off and make it
extract 6.5 and d separately?

Regards,

Pete
Sep 9 '08 #1
5 2124
On Tue, 09 Sep 2008 12:32:33 +0200, Alf P. Steinbach wrote:
Not that I know: it amounts to defining new syntax for double literal.

It might be possible to fool stream into thinking 'e' is separator, but
that would be very ugly hack, and probably not well-defined by standard.

I'd just use a loop to copy characters while digit or radix point; much
less work than finding out if hack works and whether supported by
standard. :-)
Hi, just a bit of playing with the simple code,

double a = 4.3e;

is not a valid definition because there should be an exponent after the
'e', but is there sth else defined for istringstream class, because it
can extract 6.5e even if it is not a valid expression outside the double
quotes, trying a string such as 6.5e is extracted as 6.5 only and 6.5ee
as 6.5 and e...
Sep 9 '08 #2
On Sep 9, 5:53 pm, utab <umut.ta...@gma il.comwrote:
On Tue, 09 Sep 2008 12:32:33 +0200, Alf P. Steinbach wrote:
Not that I know: it amounts to defining new syntax for
double literal.
It might be possible to fool stream into thinking 'e' is
separator, but that would be very ugly hack, and probably
not well-defined by standard.
I'd just use a loop to copy characters while digit or radix
point; much less work than finding out if hack works and
whether supported by standard. :-)
Hi, just a bit of playing with the simple code,
double a = 4.3e;
is not a valid definition because there should be an exponent
after the 'e', but is there sth else defined for istringstream
class, because it can extract 6.5e even if it is not a valid
expression outside the double quotes, trying a string such as
6.5e is extracted as 6.5 only and 6.5ee as 6.5 and e...
I don't think that this is right, although interpreting the
standard literally... It looks like there's an error in the
standard. This is certainly not what one would want or expect;
a stream should never silently drop a non white space character.
(Most implementations do the right thing, and set fail in this
case.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 10 '08 #3
On Sep 10, 9:59 am, James Kanze <james.ka...@gm ail.comwrote:
On Sep 9, 5:53 pm, utab <umut.ta...@gma il.comwrote:
On Tue, 09 Sep 2008 12:32:33 +0200, Alf P. Steinbach wrote:
Not that I know: it amounts to defining new syntax for
double literal.
It might be possible to fool stream into thinking 'e' is
separator, but that would be very ugly hack, and probably
not well-defined by standard.
I'd just use a loop to copy characters while digit or radix
point; much less work than finding out if hack works and
whether supported by standard. :-)
Hi, just a bit of playing with the simple code,
double a = 4.3e;
is not a valid definition because there should be an exponent
after the 'e', but is there sth else defined for istringstream
class, because it can extract 6.5e even if it is not a valid
expression outside the double quotes, trying a string such as
6.5e is extracted as 6.5 only and 6.5ee as 6.5 and e...
I don't think that this is right, although interpreting the
standard literally... It looks like there's an error in the
standard. This is certainly not what one would want or expect;
a stream should never silently drop a non white space character.
(Most implementations do the right thing, and set fail in this
case.)
Just a follow-up: there is an active DR on this: DR23 (one of
the oldest around). It is apparently still under review. (The
actual DR concerns behavior in case of overflow, but the
proposed changes in wording would require reading something like
"1.23ex" into a floating point to set failbit and store 0.0 in
the result.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 10 '08 #4
On Wed, 10 Sep 2008 00:59:15 -0700, James Kanze wrote:
>
I don't think that this is right, although interpreting the standard
literally... It looks like there's an error in the standard. This is
certainly not what one would want or expect; a stream should never
silently drop a non white space character. (Most implementations do the
right thing, and set fail in this case.)
It was also strange for me, but I could not get the conclusion from your
explanation, I got the previous results with g++, intel compiler reads
another character into(unrecogniz able on cout output, but there is sth)
'fit()' which is given in the original code.

Thanks
Sep 10 '08 #5
On Sep 10, 7:18 pm, utab <umut.ta...@gma il.comwrote:
On Wed, 10 Sep 2008 00:59:15 -0700, James Kanze wrote:
I don't think that this is right, although interpreting the
standard literally... It looks like there's an error in the
standard. This is certainly not what one would want or
expect; a stream should never silently drop a non white
space character. (Most implementations do the right thing,
and set fail in this case.)
It was also strange for me, but I could not get the conclusion
from your explanation, I got the previous results with g++,
intel compiler reads another character into(unrecogniz able on
cout output, but there is sth) 'fit()' which is given in the
original code.
See my follow-up to my own posting. This is clearly an error in
the standard; g++ simply implements exactly what was written,
even though it was obviously wrong, and inconsistent with
existing practice. All of the other compilers I have access to
(VC++, Sun CC, both with the RogueWave library and with STLPort)
behave "correctly" ; that is, they do what one would expect, and
what the proposed resolution to the DR requires, and set
failbit.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 11 '08 #6

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

Similar topics

1
1657
by: blix | last post by:
When I output my char to a stream and it has a value of 11, it writes a ^B (vertical tab). This is understandable. When I attempt to stream it back into a char variable, it skips it because a tab is considered whitespace. If I cast the char to int, it'll stream out an 11 so that I can read it in plain english. Doing this same cast while streaming the value in doesn't read the value. I have to use a temporary int variable and then assign...
24
2769
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
2
2025
by: Sonny | last post by:
Hi experts, I am writing a small code to read input from a file, for example my input file is: X Y Z 26.0 28.0 0.0 32.0 8.0 0.0 My code is: #include<iostream>
15
2223
by: Adam H. Peterson | last post by:
I would like to make a stream or streambuf that tracks the number of lines that have been read and stuff like that (so, for example, when I get an error message, I can ask the stream for the line number and put it in error messages. I tried to create a streambuf class that held an instance of another streambuf (through parameterized inheritance) and would ferry calls from the one to the other, tracking data as it was read, but I wasn't...
1
1328
by: yeutim | last post by:
I can't find anything wrong this with this class. Especially, Overloading Stream-Extraction Operator (in blue). Any one know what do I need to fix this problem please reply. Thank you for your help! --------------------------------------------- class MyData{
9
2431
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john doe", with a space in it). For console input, cin.getline(var, howMuchIWant) or cin.get() has done the trick for me in the past. It doesn't seem to work for me nearly so well with a file stream. I wouldn't have thought cpp regarded
2
7379
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
1
1697
by: beatTheDevil | last post by:
Hello all, I have a question that concerns how C++ input streams (istream, ifstream, istringstream, etc.) behave using the extraction (>>) operator when at the end of a stream's contents. For instance, I have an input file which is just a one-line string: "do_stuff 20060101 " (yes, that is a trailing space) I'm using a parse function that attempts to extract a string ("do_stuff") and a long ("20060101") by using some code roughly...
33
2065
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: // ... bool good() const; // next operation might succeed bool eof() const; // end of input seen
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.