[PPL-devel] Silently passing wrong input for good one
Roberto Bagnara
bagnara at cs.unipr.it
Fri Oct 21 19:11:24 CEST 2005
Hi there,
the program below, when compiled with GCC 4.0.2 and
given the input "-3e-abcd", prints
input = `-3e-abcd'
good
f = -3
left = `abcd'
which means that:
- if you give "-3e-abcd" to a `cin >> f', the double -3.0 is read;
- the characters "-3e-" are eaten;
- the status of `cin' is set to "good".
I was unable to understand whether this behavior is conforming
or even mandated by the standard. To me it does not look good,
for the reason that "-3e-", the string eaten, would not seem
to be a valid floating point numeral. Nonetheless, the status
is set to good.
For comparison, wit Comeau C++ I get
$ como z.cc
Comeau C/C++ 4.3.3 (Oct 24 2003 16:00:23) for RedHat_LINUX_INTEL_ELF
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:non-strict warnings C++
$ a.out
-3e-abcd
input = `-3e-abcd'
fail
left = `abcd'
At least here the programmer has a way to know that the input was wrong,
something that, with the behavior of libstdc++, would seem impossible
(since an extra "e-" has been silently and undetectably eaten).
So the question is: are both behaviors conforming to the standard?
It not, who is wrong?
If yes, isn't the behavior of Comeau's library more robust and useful?
All the best,
Roberto
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
char input[200];
cin.getline(input, sizeof(input));
cout << "input = `" << input << "'" << endl;
stringstream in(input);
double f = -123.456;
in >> f;
string s = "initialized";
if (in.eof())
cout << "eof" << endl;
if (in.fail())
cout << "fail" << endl;
if (in.good())
cout << "good" << endl;
if (in.bad())
cout << "bad" << endl;
char left[200];
if (in) {
cout << "f = " << f << endl;
in.getline(left, sizeof(left));
cout << "left = `" << left << "'" << endl;
}
else {
in.clear();
in.getline(left, sizeof(left));
cout << "left = `" << left << "'" << endl;
}
}
--
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara at cs.unipr.it
More information about the PPL-devel
mailing list