Why does Qt Creator add a newline after cout.flush() call?
Asked Answered
T

1

7

The code is simple

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
    for(int i = 0; i < 3; ++i)
    {
        cout << "1 "; cout.flush();
        sleep(1);
    }
}

while in .pro file

QT += core    
TARGET = ProjectName    
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets    
TEMPLATE = app

If the output goes into Qt Creator "application output" (the place under source code place by default), every "1" starts from a new line.

If I compile and execute separate *.cpp file in Ubuntu Terminal using g++, it works in a proper way.

If I create a new project in Qt Creator and output goes into Qt Creator console (new window with black background), it works in a proper way.

Well, why does cout.flush() cause a newline in the first case?

Thalassa answered 29/11, 2014 at 13:4 Comment(0)
L
6

Apparently, this is a Qt Creator bug that they don't plan to resolve any time soon (at least that was the state in April, 2015). I've tested Qt Creator 3.5.1 (Qt 5.5.1) and the bug still exists, even though the new line is printed only after the first std::cout.flush() call or appending std::flush manipulator to std::cout << call.

Anyways, JIRA ticket for this bug can be found here:

Flushing application output automatically starts a new line

Lightship answered 28/10, 2015 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.