gpt4 book ai didi

c++ - 读入 char 变量后 QTextStream 不可用

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:23:41 25 4
gpt4 key购买 nike

所以这是我正在编码的任务的客户端部分。

#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 的输入,程序就会设置坐标,然后一个接一个地打印输出的其余部分,不需要任何输入。这是输出。

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

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

最佳答案

Qt 有一个错误 QTBUG-37394 ,而且您的代码也有错误 :)

  • 您的错误:当将单个字符读入 charQChar 时,您将读取终止任何前面输入的空白​​。您必须使用 ws(stream) 去除流中的现有空白。

  • Qt 错误:字符读取运算符等待非空白数据,即使输入流中已经有可用的空白字符也是如此。

详情

它不起作用的原因是您作为角色消费的是行的换行符。

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

    { '2', '0', '\n' }
  2. cin >> arcseconds运行后,字符缓冲区为:

    { '\n' }
  3. 然后,cin >> cardinalDirection 运行,并等待新的输入,因为缓冲区中没有任何有趣的内容(这是 Qt 错误:它不应该等待)。假设你输入N,字符缓冲区现在是:

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

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

    { 'N', '\n' }
  4. 问题是现在您读取的是经度。缓冲区包含非空白数据,以下立即执行:cin >> degrees

    正如预期的那样,将 N 读入整数失败,并且不会处理进一步的输出。

    如果你输入一个数字,比如说 1,它会成功,当它要求度、分、秒、基本方向然后要求分钟时,你会有奇怪的交换跳过学位。

修复方法是强制跳过数据中的空白。 QTextStream::ws() 方法的文档中有这样一句话:

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;
}

关于c++ - 读入 char 变量后 QTextStream 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22310207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com