cin does not read input properly
-
I am just starting with QT6 (and with QT in general).
I have tried to do simpe cin/cout operations and it's already troublesome. Somehow cin does not read a line properly, nor does getline.
Here is my code:
#include <QCoreApplication> #include <iostream> #include <string> using namespace std; void do_something() { string txt = ""; cout << "Bitte input: "; getline(cin, txt); cout << "Das war der input " << txt << endl; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); do_something(); return a.exec(); }
I run the program in the terminal not in the QT Creator output!
My Problem is, that when i remove the endl from the second cout, cin does not read any text and the program is stuck.
-
You assumption is wrong.
std::endl forces flushing the output buffer. Without this you may not see what you've typed in because the buffer is not flushed at all. Don't see what this has to do with Qt though. -
with your suggestion i have tried to flush the output buffer manually. that did not work.
In addition i have tried to use just the same code snippet, put it into a .cpp file, compiled it and it just does what i expect it to do
when i debugg it, it does get stuck in the line of "getline(cin, txt);" and it never reaches the cout in the next line. so why would that have anything to do with the output buffer?
EDIT:
actually i have tried to flush the output before the cout command at the end. that did not work.
but flushing after that did indeed help!What i still don't know is, why the debugger cannot go over the "getline.." line.
And why does flushing the buffer before cout not help? -
@tiegert said in cin does not read input properly:
And why does flushing the buffer before cout not help?
Why should flushing the buffer before actually writing something to it should help?
-
@JonB i do run the output in the Terminal, and not in the Qt Creators Output console. There is an option under Project->Run and then set "Run in Terminal"
@Christian-Ehrlicher At first i had a wrong understanding of flush, but i looked it up.
For everyone who reads this thread and is wondering what it does:
When you try to write something to the terminal, the single characters or words are not immediatly written to the terminal.
They get buffered until it gets decided that the text should now be shown in the terminal.
The decicion to output the text to the terminal and clear the buffer for new input is made by flush or implicitly by std:endl.What confused me a lot in the first place was, that when using cpp outside of Qt i did not have to do that.
But the Terminal in which i ran my program, always shows some output after my program ran.
I guess that the Terminal i use flushes the buffer when displaying text and my output also get's shown then. -
@tiegert This is a life saver. Who would have thought we need to check that little "Run in terminal" checkbox after clicking the Projects icon on the left. This should have been the default. Otherwise where do we see the output from our "hello world" app? If we see that in the Application Output, naturally we would attempt to key in our data there. If Application Output for output only then make the window read-only. Still suffering from the first experience....can't even run the app outside of Qt Creator as lots of missing dlls...I guess I have been spoiled by Microsoft....