Extra newline in output

Hi all,

I am writing simple command line programs using Visual Studio 2017 Community and compiling and linking through the Developer Command Prompt. My question is, is there a setting that adds an extra newline when the program is finished. For example:


1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World";
    return 0;
}


produces the output

Hello World
<command prompt here>

However, if the cout statement becomes

 
cout << "Hello World\n";


then the output becomes:
Hello World

<command prompt here>

What I would like to happen is for a newline to only be output when I actually give the command to do so, instead of the prompt doing it for me. Thus, this is what I would like:

 
cout << "Hello World\n";


Hello World
<command prompt here>

Again, is there a setting that I can change to reflect this?
No, that behavior comes from either cmd.exe or conhost.exe. There's no way to change it. If I run your first version on a Linux machine I get
Hello Worldhelios@linux:~$
With your second version I get
Hello World
helios@linux:~$


For what it's worth, IMO a program sending output to stdout should always send a newline as the final character before exiting regardless of the platform, unless there's a very compelling reason not to do that.
Last edited on
Thank you for your reply. I also was trying to emulate the output in a Unix/Linux environment. So I guess I'll just have to live with it. Way to go Microsoft......
Topic archived. No new replies allowed.