c++ - QTextStream unusable after reading into a char variable -
so client part of assignment i'm coding.
#include "coordinate.h" #include "gpscoord.h" #include <qstring> #include <qtextstream> #include <qcoreapplication> qtextstream cout(stdout); qtextstream cin(stdin); int main(int argc, char* argv[]) { qcoreapplication app(argc, argv); gpscoord gps; int degrees, minutes, seconds; char cardinaldirection; cout << "\nplease enter values latitude.." << endl; cout << "degrees : " << flush; cin >> degrees; cout << "minutes : " << flush; cin >> minutes; cout << "seconds : " << flush; cin >> seconds; cout << "cardinal direction : " << flush; cin >> cardinaldirection; gps.setcoord(degrees, minutes, seconds, cardinaldirection); cout << "\nplease enter values longitude.." << endl; cout << "degrees : " << flush; cin >> degrees; cout << "minutes : " << flush; cin >> minutes; cout << "seconds : " << flush; cin >> seconds; cout << "cardinal direction : " << flush; cin >> cardinaldirection; gps.setcoord(degrees, minutes, seconds, cardinaldirection); cout << "\ngeographic coordinates\t: " << gps.tostring(false) << endl << "decimal coordinates\t: " << gps.tostring(true) << endl; return 0; } the first half works fine. enter input first cardinaldirection, program sets coordinate prints rest of output 1 after other , doesn't want input. here output.
please enter values latitude.. degrees : 25 minutes : 46 seconds : 3 cardinal direction : s please enter values longitude.. degrees : minutes : seconds : cardinal direction : geographic coordinates : 0ø, 0', 0", s ; 25ø, 46', 3", decimal coordinates : 0 ; 25.775 is stupid i'm missing? can't imagine cause this.
qt has bug qtbug-37394, , code has bug :)
your bug: when reading single characters either
charorqchar, read whitespace terminated preceding input. must usews(stream)rid of existing whitespace in stream.qt bug: character-reading operators wait non-whitespace data if there whitespace character available in input stream.
details
the reason doesn't work consume character newline previous line.
after enter, say, 20 arcseconds, contents of character buffer are:
{ '2', '0', '\n' }after
cin >> arcsecondsruns, character buffer is:{ '\n' }then,
cin >> cardinaldirectionruns, , waits new input since there's nothing interesting in buffer (that's qt bug: shouldn't wait). suppose entern, character buffer now:{ '\n', 'n', '\n' }now,
operator>>(char&)correctly retrieves first character in buffer, no matter is. retrieves'\n', succeeds (that's bug: should rid of whitespace first). character buffer contains:{ 'n', '\n' }the problem read longitude. buffer contains non-whitespace data, , following executes immediately:
cin >> degrees.as expected, reading
ninteger fails, , no further output processed.if you'd enter digit,
1, succeed, , have curious exchange when asks degrees, minutes, seconds, cardinal direction , asks minutes while skipping degrees.
the fix forcibly skip whitespace in the data. documentation qtextstream::ws() method has telling sentence:
this function useful when reading stream character character.
#include <qtextstream> #include <qcoreapplication> qtextstream cout(stdout); qtextstream cin(stdin); class gpscoord { qstring dir; public: void setcoord(int, int, int, char d) { dir.append(d); } qstring tostring(bool) const { return dir; } }; int main(int argc, char* argv[]) { qcoreapplication app(argc, argv); gpscoord gps; int degrees, minutes, seconds; char cardinaldirection; cout << "\nplease enter values latitude.." << endl; cout << "degrees : " << flush; cin >> degrees; cout << "minutes : " << flush; cin >> minutes; cout << "seconds : " << flush; cin >> seconds; cout << "cardinal direction : " << flush; ws(cin) >> cardinaldirection; gps.setcoord(degrees, minutes, seconds, cardinaldirection); cout << "\nplease enter values longitude.." << endl; cout << "degrees : " << flush; cin >> degrees; cout << "minutes : " << flush; cin >> minutes; cout << "seconds : " << flush; cin >> seconds; cout << "cardinal direction : " << flush; ws(cin) >> cardinaldirection; gps.setcoord(degrees, minutes, seconds, cardinaldirection); cout << "\ngeographic coordinates\t: " << gps.tostring(false) << endl << "decimal coordinates\t: " << gps.tostring(true) << endl; return 0; }
Comments
Post a Comment