3
votes

I'm somewhat new to C++ and QT. Trying to run a very simple program in QtCreator, which uses console input on 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;
}

Problem is that native QTCreator's application output, for it's name, doesn't support typing in things. Here is application output:

Starting C:\QProject\test-build-desktop-Qt_4_8_0_for_Desktop_-MinGW_Qt_SDK___>z>>\debug\test.exe...

This is a string.

The length of that string is 17

Enter a sentence with whitespaces:

Qml debugging is enabled. Only use this in a safe environment!

I've tried checking "Run in terminal" in Projects>Desktop>Run as some answers to similar questions here suggested and the terminal shows up, but it doesn't seem to interact with the program anyhow. Terminal's output:

Press RETURN to close this window...

1
Could you put your cin and cout in main's scope to see if that makes a difference? (Not the best naming idea in general, people will assume they're std::cin/out.)Mat
Yup, tried to make my question clearer, thanks.user1230585

1 Answers

2
votes

I would say that checking Run in terminal is correct and needed.

What is surprising is that you don't get any compile error, as there is a mistake at line 8 :

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

The last << is wrong.

Correcting your code, I get this :

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

which works fine on my computer (WinXP, QtCreator 2.2.0).

Are you sure your Qt project is correct and that you are compiling the right file ?