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

operator>> for numbers: stream state after failed read


Summary:
Does the C++ standard require
std::basic_istream<>::operator>>(double&) to leave the input stream
untouched in case of a read failure?

Details:
I noticed an unexpected behavior of operator>>() for numbers (double,
int) when reading from cin. I would like to ask for expert
clarification on whether I am misunderstanding the rules of the game,
or whether my library implementation has a bug. I tested this on g++
4.1.1 under Linux, g++ 3.4.5 MinGW and cxx 7.1 under Tru64 Unix
(behavior there is slightly different than described below). I checked
Josuttis "The C++ Standard Library" and the C++ standard ch 22.2.2 and
27.6.1, but haven't been able to get anything useful out of them.

The problem is as follows: If would like to read sequences like "1 2
+" by first trying to read into a double, and if that fails try to
read into a char, see sample program below (the program reads only a
single number/symbol). This works fine as long as any non-number token
is not a symbol that could be the first symbol in a number, i.e. the
plus sign, the minus sign or the decimal point. If the symbol is one
of those three, the program simply hangs. If I change the locale to,
e.g., Norwegian, it will hang on the decimal comma instead of the
decimal point.

My interpretation is that the operator reads +, -, or ., then tries to
read the next digit, which it does not find, and then raises the
failbit and returns WITHOUT putting +, -, or . back into the input
stream. Should this/must this be so?

A work-around presumably is to read to a string, place it into a
stringstream and then extract from the latter.

I'd appreciate expert advice on this.
Hans

/*
Illustrate problems with symbols that can
be prefixes to numbers (+,-,. if no locale is set).

If a number is entered on the keyboard, the program prints
OK --- double: -1.3

If any characted but + - . is entered, the program prints
FAIL --- double
OK --- char: a

If + - or . are entered, the program hangs after
FAIL --- double
*/

#include <iostream>
using namespace std;
int main()
{
double x = 0;
char c = 0;

if ( cin >x )
cerr << "OK --- double: " << x << endl;
else
{
cerr << "FAIL --- double" << endl;;
cin.clear();
if ( cin >c )
cerr << "OK --- char: " << c << endl;
else
cerr << "FAIL --- char" << endl;
}

return 0;
}

Mar 21 '07 #1
0 1690

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

Similar topics

1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
4
by: Dan | last post by:
Hi, I would just like to know if the istream operator takes only one parammeter(object) at a time (like z) ? istream operator>>(istream& in, Shape &z) Cause I keep getting error concerning the...
4
by: Ook | last post by:
I'm a but fuzzy about this. You would call it like this:Polynomial poly; // Polynomial is my class with various properties cin >> poly; In the function itself, I would, for example, do the...
3
by: ax | last post by:
to implement std::cout and file output seems simple but for file input and cin i have some question for the "std::cin" #include <iostream.h> int main(void) {int n, sum=0; while(cin >> n)...
3
by: moleskyca1 | last post by:
In C++ FAQ (15.5), I see this code: int i = 0; while (std::cin >x) { // RIGHT! (reliable) ++i; // Work with x ... } but I don't know how can while loop end? ostream &operator >>(...) return...
3
by: Daniel T. | last post by:
This is basically what I have now... class Foo { /* definition irrelevant */ }; istream& operator>>( istream& is, Foo& foo ); // could throw int main() { ifstream file( "file.txt" ); Foo...
18
by: subramanian100in | last post by:
Consider a class that has vector< pair<int, string>* c; as member data object. I need to use operator>to store values into this container object and operator<< to print the contents of the...
5
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
3
by: Lambda | last post by:
I'd like to load a text file to memory. The code is like: void load_dict() { static const string DICT("D:\\README.txt"); ifstream dict(DICT.c_str(), std::ios::in); if (!dict) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.