1

When debugging an app using std::cout to print something, nothing appears in the debug console (3rd tab, not external console). One of the exceptions seems to be the use of std::endl:

#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
    while(true) {
        Sleep(500);
        std::cout << "Hello world!" << std::endl; // Works
    }    
}
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
    while(true) {
        Sleep(500);
        std::cout << "Hello world!\n"; // Doesn't work

        // std::cout << "Hello world!"; // Doesn't work
        // std::cout << "Hello world!" << std::flush; // Doesn't work
    }    
}

In the first example our lines appear over time, while on the second doesn't appear at all (or the console refreshes somewhere around 60 secs). I think this is a bug but I'm not sure, so what is a workaround? Maybe I have to configure something instead? I found this problem while working on something else: C++: cannot see output in VS Code while debugging

Edit

The problem is what to do when u wanna output something without a new line

12
  • 2
    std::endl is \n plus std::flush, which isn't one of the options you seem to have tried. Commented May 18, 2020 at 15:48
  • 1
    \r\n should flush automatically, however this is an implicit contract with the OS. Just call std::flush. Commented May 18, 2020 at 15:49
  • 1
    @MSalters Ah yeah maybe, I know that in POSIX C it flushes automatically on end-of-line, I thought Windows had the same behaviour (legacy from command line era). Commented May 18, 2020 at 16:03
  • 1
    @sturcotte06 "in POSIX C it flushes automatically on end-of-line" that's not really true. Commented May 18, 2020 at 16:08
  • 1
    @n.'pronouns'm. , DEBUG CONSOLE is the right one, I'm sure of that, as TERMINAL shows building stuff. I'm using a launch.json to debug my code. std::flush does not have any effect without \n. I need to be able to get stuff on the console without forcing a line end, and without using an external console (because the later one doesn't make sense) Commented May 18, 2020 at 17:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.