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 char or qchar, read whitespace terminated preceding input. must use ws(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.

  1. after enter, say, 20 arcseconds, contents of character buffer are:

    { '2', '0', '\n' } 
  2. after cin >> arcseconds runs, character buffer is:

    { '\n' } 
  3. then, cin >> cardinaldirection runs, , waits new input since there's nothing interesting in buffer (that's qt bug: shouldn't wait). suppose enter n, 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' } 
  4. the problem read longitude. buffer contains non-whitespace data, , following executes immediately: cin >> degrees.

    as expected, reading n integer 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

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -