QTextStream读取一个char变量后不可用 [英] QTextStream unusable after reading into a char variable

查看:251
本文介绍了QTextStream读取一个char变量后不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我编写的作业的客户端部分。

So this is the client part of my assignment that 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 for the 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 for the 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;
}

前半部分工作正常。但是一旦我输入第一个cardinalDirection的输入,程序设置坐标,但是只是一个接一个打印输出的其余部分,不想要任何输入。这是输出。

The first half works fine. But as soon as I enter the input for the first cardinalDirection, the program sets the coordinate but then just prints the rest of the output one after the other and doesn't want for any input. Here is the output.

Please enter values for the latitude..
Degrees : 25
Minutes : 46
Seconds : 3
Cardinal Direction : S

Please enter values for the longitude..
Degrees : Minutes : Seconds : Cardinal Direction : 
Geographic Coordinates  : 0ø, 0', 0", S ; 25ø, 46', 3", 

Decimal Coordinates : 0 ; 25.775

这是愚蠢的,我错过了吗?无法想象会是什么原因造成的。

Is it something stupid that I'm missing? Can't imagine what would cause this.

推荐答案

奇怪的是:Qt有一个错误, too:)

The curious thing is: Qt has a bug, but your code has a bug too :)


  • 您的错误:当读取单个字符到 char QChar ,您将读取终止任何先前输入的空格。您必须使用 ws(stream)才能移除串流中现有的空格。

  • Your bug: When reading single characters into either char or QChar, you will read whitespace that terminated any preceding input. You must use ws(stream) to get rid of existing whitespace in the stream.

它不起作用的原因是,您作为字符使用的是上一个行的换行符。

The reason it doesn't work is that what you consume as a character is the newline from the previous line.


  1. 输入后,例如20角秒,字符缓冲区的内容为:

  1. After you enter, say, 20 arcseconds, the contents of the character buffer are:

{ '2', '0', '\n' }

/ li>

  • cin>> arcseconds 运行,字符缓冲区为:

  • After cin >> arcseconds runs, the character buffer is:

    { '\n' }
    


  • 然后, cin> cardinalDirection 运行,并等待新的输入,因为缓冲区中没有什么有趣的东西(这是Qt的bug:它不应该等待)。假设你输入 N ,字符缓冲区现在是:

  • Then, cin >> cardinalDirection runs, and waits for new input since there's nothing interesting in the buffer (that's the Qt bug: it shouldn't wait). Suppose you enter N, the character buffer is now:

    { '\n', 'N', '\n' }
    

    正确地正确地检索缓冲区中的第一个字符,无论它是什么,c> operator>>>(char&)因此,它检索'\\\
    '
    并成功(这是你的错误:你应该先摆脱空格)。字符缓冲区现在包含:

    Now, operator>>(char&) correctly retrieves the first character in the buffer, no matter what it is. Thus it retrieves '\n' and succeeds (that's your bug: you should get rid of the whitespace first). The character buffer now contains:

    { 'N', '\n' }
    


  • 问题是,现在你读的经度。缓冲区包含非空格数据,并且以下内容立即执行 cin>> $

    如预期,将 N 读入整数失败,

    As expected, reading an N into an integer fails, and no further output is processed.

    如果输入一个数字,说 1 ,它会成功,

    If you'd enter a digit, say 1, it will succeed, and you will have the curious exchange when it asks for degrees, minutes, seconds, cardinal direction and then asks for minutes while skipping degrees.

    修复是强制跳过数据中的空格。 QTextStream :: ws()方法的文档具有这个句子:

    The fix is to forcibly skip the whitespace in the the data. The documentation for QTextStream::ws() method has this telling sentence:


    当按字符读取流时,此函数非常有用。

    This function is useful when reading a stream character by 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 for the 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 for the 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;
    }
    

    这篇关于QTextStream读取一个char变量后不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆