Reputation: 43
When I try to get input from a Qt console application the program is not working as I expect it to. The following works as expected:
#include <QCoreApplication>
#include <QTextStream>
QTextStream cout(stdout);
QTextStream cin(stdin);
int main()
{
QString msg("Hello world!");
cout << msg << endl;
return 0;
}
Output:
Hello world!
But as soon as I add,
...
int main()
{
QString msg("Hello world!");
cout << msg << endl;
cout << "Enter new message: ";
msg = cin.readLine();
cout << endl << msg << endl;
return 0;
}
Output:
Hello world!
is displayed but then the program waits for the input before displaying the prompt to enter text, instead of first displaying the prompt and THEN reading the input. The entered text displays together with (after) the prompt when entered.
I've been trying to resolve this for a couple of hours to no avail.
Upvotes: 4
Views: 2779
Reputation: 29285
Also you can use:
cout << "Enter new message: " << flush;
std::flush
just flushes the stdout
stream.std::endl
besides adding a new line to the stdout
, it also flushes the stream.Upvotes: 1