- Understanding invalid user input - 8 Updates
| Joseph Hesse <joeh@gmail.com>: Nov 10 10:50AM +0200 /* I am trying to understand the intricacies of invalid user input to a statement like "cin >> x" where x is of some data type. I wrote the following function, "PrintBuffer" to print the internal buffer of cin. Here is a run of the program $ ./a.out Enter Double xx2.5yy You entered x = 0 xx2.5yy ^C $ It sort of worked, it showed the buffer but not the final newline and it hung so I had to ^C to end the program. My question is, how do I fix the function? Thank you, Joe */ ================================================ #include <iostream> using namespace std; void PrintBuffer(istream &s) { cout << s.rdbuf() << "--" << endl; } int main() { double x{17.5}; cout << "Enter Double" << endl; cin >> x; cout << "You entered x = " << x << endl; PrintBuffer(cin); return 0; } |
| "Öö Tiib" <ootiib@hot.ee>: Nov 10 02:17AM -0800 On Wednesday, 10 November 2021 at 10:50:58 UTC+2, Joseph Hesse wrote: > PrintBuffer(cin); > return 0; > } Posted program shouldn't produce described output. The "--" is clearly missing from that. Maybe you run some other program or some previous version of it that was designed to hung? |
| Ben Bacarisse <ben.usenet@bsb.me.uk>: Nov 10 11:09AM >> ^C >> $ >> It sort of worked, it showed the buffer but not the final newline and it It does show the newline, but they are hard to see! If the newline were not being shown it, you'd see $ ./a.out Enter Double xx2.5yy You entered x = 0 xx2.5yy^C $ >> hung so I had to ^C to end the program. It's waiting for input. You can use ^D to close the input stream and you will get the rest of the output. >> My question is, how do I fix the function? Hm... I don't think there is a well-defined way to see the content of the internal buffer, but you can show the progress of the input in other ways. For example using gcount(). > Posted program shouldn't produce described output. The "--" is clearly > missing from that. Maybe you run some other program or some > previous version of it that was designed to hung? I get the reported behaviour. After printing the stream buffer, the program is waiting for input. Closing the input (usually ^D) causes the -- to be output. I can't see what cout << s.rdbuf() is defined to do. It appears consumes input rather than simply printing the buffer contents. -- Ben. |
| Bonita Montero <Bonita.Montero@gmail.com>: Nov 10 03:11PM +0100 Better use C++17's from_chars(). #include <iostream> #include <charconv> #include <utility> #include <cstring> using namespace std; int main() { auto parseDouble = []( char const *str ) -> pair<bool, double> { double value; from_chars_result fcr = from_chars( str, str + strlen( str ), value ); if( fcr.ec != errc() || *fcr.ptr ) return pair<bool, double>( false, 0.0 ); return pair<bool, double>( true, value ); }; cout << "enter value" << endl; string dStr; cin >> dStr; pair<bool, double> pd = parseDouble( dStr.c_str() ); if( !pd.first ) { cout << "invalid value" << endl; return EXIT_FAILURE; } cout << "you entered: " << pd.second << endl; } |
| Bonita Montero <Bonita.Montero@gmail.com>: Nov 10 03:31PM +0100 Better solution: #include <iostream> #include <charconv> #include <utility> #include <cstring> #include <limits> using namespace std; int main() { auto parseDouble = []( string const &str ) -> double { double value; from_chars_result fcr = from_chars( str.c_str(), str.c_str() + strlen( str.c_str() ), value ); if( fcr.ec != errc() || *fcr.ptr ) return std::numeric_limits<double>::quiet_NaN(); return value; }; cout << "enter value" << endl; string dStr; cin >> dStr; double d = parseDouble( dStr ); if( isnan( d ) ) { cout << "invalid value" << endl; return EXIT_FAILURE; } cout << "you entered: " << d << endl; } |
| Joseph Hesse <joeh@gmail.com>: Nov 10 05:15PM +0200 On 11/10/21 12:17, Öö Tiib wrote: > Posted program shouldn't produce described output. The "--" is clearly > missing from that. Maybe you run some other program or some > previous version of it that was designed to hung? It does produce the output I gave. I cut the program out of the posting, compiled it with g++ and executed a.out. I am running Fedora 35 Linux. |
| "Alf P. Steinbach" <alf.p.steinbach@gmail.com>: Nov 10 05:20PM +0100 On 10 Nov 2021 15:11, Bonita Montero wrote: > } > cout << "you entered: " << pd.second << endl; > } When you parse the input string you can use `std::getline` instead of `>>` formatted input. Conversely, when you use `>>` you can let `>>` parse the input. I see no advantage in both using `>>` and parsing the input. - Alf |
| DozingDog@thekennel.co: Nov 10 05:12PM On Wed, 10 Nov 2021 15:31:52 +0100 > } > cout << "you entered: " << d << endl; >} Or alternatively dump most of that over complicated crap and just use strtof() C++ reinventing the wheel once aqain. |
| You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment