[SOLVED] In Qt6 cin/getline does not read any input for me


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, nor does getline.

Here is my code:

#include <QCoreApplication>
#include <iostream>
#include <string>

using namespace std;

void do_something()
    string name = "";
    cout << "Enter your name: \n";
    cin >> name;
    cout << "Hello " << name << "\n";

int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    return a.exec();

The line "Enter your name: " get’s written, the newline character does not!?
Then whatever i type in the console, does not do anything to cin, and the program is, as it seems stuck in cin.

If instead of "\n" i use "endl" for the "\n" at the second cout, the following happens.
Cin is completely getting ignored and the "Hello" also gets printed.


The solution was, as written in the comments:

  1. Run the program in the Terminal, not in the QT Creator’s Output Termina.
    In the output Terminal you cannot type any input.

  2. Flush the buffer because the text lies in the buffer until a endl or a flush occurs.

Answered By – Michael Hübler

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *