首页 文章

QtCreator 2.4.1控制台输入

提问于
浏览
3

我对C和QT有些新意 . 试图在QtCreator中运行一个非常简单的程序,它在WinXP上使用控制台输入:

#include <QString>
#include <QTextStream>

int main() {
    QTextStream streamOut(stdout);
    QTextStream streamIn(stdin);
    QString s1("This "), s2("is a "), s3("string.");
    QString s4 = s1 + s2 + s3;
    streamOut << s4 << endl;
    streamOut << "The length of that string is " << s4.length() << endl;
    streamOut << "Enter a sentence with whitespaces: " << endl;
    s4 = streamIn.readLine();
    streamOut << "Here is your sentence: \n" << s4 << endl;
    streamOut << "The length of your sentence is: " << s4.length() << endl;
    return 0;
}

问题是原生QTCreator的应用程序输出,因为它的名称,不支持输入内容 . 这是应用程序输出:

启动C:\ QProject \ test-build-desktop-Qt_4_8_0_for_Desktop_-MinGW_Qt_SDK ___> z >> \ debug \ test.exe ...这是一个字符串 . 该字符串的长度为17输入带有空格的句子:启用Qml调试 . 只能在安全的环境中使用它!

我已经尝试在项目>桌面>运行中检查“在终端中运行”,因为这里建议的类似问题的一些答案和终端显示,但它似乎无论如何都不与程序交互 . 终端输出:

按RETURN关闭此窗口...

1 回答

  • 2

    我会说检查 Run in terminal 是正确的并且需要 .

    令人惊讶的是你没有得到任何编译错误,因为第8行有一个错误:

    cout << "Enter a sentence: "<<;
    

    最后 << 错了 .

    纠正你的代码,我明白了:

    #include <QString>
    #include <QTextStream>
    QTextStream cout(stdout);
    QTextStream cin(stdin);
    
    int main() {
        QString s2;
        cout << "Enter a sentence: ";
        s2 = cin.readLine();
        cout << "Here is your sentence:" << s2 << endl;
        cout << "The length of your sentence is: " << s2.length() << endl;
        return 0;
    }
    

    它在我的电脑上工作正常(WinXP,QtCreator 2.2.0) .

    您确定您的Qt项目是否正确并且您正在编译正确的文件?

相关问题