- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以这是我正在编码的任务的客户端部分。
#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 ,而且您的代码也有错误 :)
您的错误:当将单个字符读入 char
或 QChar
时,您将读取终止任何前面输入的空白。您必须使用 ws(stream)
去除流中的现有空白。
Qt 错误:字符读取运算符等待非空白数据,即使输入流中已经有可用的空白字符也是如此。
它不起作用的原因是您作为角色消费的是前行的换行符。
输入 20 角秒后,字符缓冲区的内容为:
{ '2', '0', '\n' }
cin >> arcseconds
运行后,字符缓冲区为:
{ '\n' }
然后,cin >> cardinalDirection
运行,并等待新的输入,因为缓冲区中没有任何有趣的内容(这是 Qt 错误:它不应该等待)。假设你输入N
,字符缓冲区现在是:
{ '\n', 'N', '\n' }
现在,operator>>(char&)
正确 检索缓冲区中的第一个字符,无论它是什么。因此它检索 '\n'
并成功(这是你的错误:你应该首先摆脱空白)。字符缓冲区现在包含:
{ 'N', '\n' }
问题是现在您读取的是经度。缓冲区包含非空白数据,以下立即执行: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/
我一直在玩一些 c++ hello world 的东西,并认为我会快速获得 qt 字符串。回显和转换字符串很好,但是 endl似乎很特别: #include int main(int, char*[
我想查看读取 QFile 的 QTextStream 的下一个字符,以创建一个高效的标记器。 但是,我没有找到任何令人满意的解决方案。 QFile f("test.txt"); f.open(QIOD
如何在标题中设置一个新的 QTextStream,就像这样 MainClass{ private: QTextStream out; } 然后,在初始化设置文件,而不是 QTextStream
我正在打开一个文件并获取它的 QTextStream。然后我使用 readLine() 逐行读取流。当该行与某个字符串匹配时,我需要将其替换为另一个字符串。我需要的行为是完全替换该行(即,如果该行是“
我正在学习 QtCreator,但在向文件中写入数据时遇到了问题。这是一些代码: char *str; int i = 1; QFile outFile("path/to/file/ou
我只是关注这个主题“How to print to console when using Qt”,以使我的应用程序正常运行。我开始怀疑 - 是否可以为单个 QTextStream 订阅更多输出?例如,
我尝试从同一个文件读取和写入,但出现奇怪的行为。这是示例代码: 主窗口.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include
我的问题很简单,但我无法解决... 在我的标题中: QTextStream *in = NULL; 在一种方法中,QTextStream 被初始化: in = new QTextStream(&fil
我在学习 Qt,使用 Qt5.1,我在重音方面有编码问题。我用葡萄牙语编写带有输出的程序,并且总是必须使用重音符号。该问题可以在以下代码中演示: \#include \#include \#inc
我正在为文本文件编写解析器,但 QTextStream 似乎缺少某些内容。我使用的是 Qt 5.4.1,代码是单线程的,如果这很重要的话。 这是方法: const Event* AviLogFile:
我有这几行代码: QFile file("h:/test.txt"); file.open(QFile::ReadOnly | QFile::Text); QTextStream in(&file);
我正在尝试使用 QTextStream 来格式化数字输出。我已将我的问题简化为一个小测试程序来演示我的问题。以下是我的代码: #include #include #include int mai
我的文件名中有空格,但由制表符分隔。我如何使用 QTextStream 一个一个地阅读它们? 正常的方式会被制表符和空格分割(实际上是任何 QChar::isSpace()),这不是我想要的: QSt
我正在使用以下代码将一些文本写入文件: QFile caFile(outputFolder + "file.extension"); caFile.open(QIODevice::WriteOnly
我想在 linux 机器上读取和解析/proc/PID/status 文件的内容,但 QTextStream.atEnd 在开始读取时总是返回 true。编码: QString procDirecto
我正在使用 QTextStream 将数据写入文件。它组织得很好(几乎完美),例如: i t y yy 0 0.016
我使用以下代码查找从 QFile 中读取的字节数。对于某些文件,它给出了正确的文件大小,但对于某些文件,它给出了一个近似于 fileCSV.size()/2 的值。我正在发送两个文件,其中的字符数相同
readLine()后,如何设置光标位置到行首? 使用 seek() 和 pos() 对我不起作用。 这是我的 file.txt 的样子: Object1 Some-name 2 3.40 1.50
我正在使用 QTextStream 写入 QFile,一切正常。我正在尝试创建一些错误检测,因此我尝试删除写入之间的输出文件。 扼杀,Qtextstream的状态继续显示0(无错误),QFile的er
我需要从 foo.txt 中读取字符串: #include #include #include #include int main( int argc, char** argv ) {
我是一名优秀的程序员,十分优秀!