So I’m just starting to learn C++, and I’m just trying to get everything set up. When I run the compiled binary for my hello world code, it display’s the console output, but doesn’t switch to a new line afterwards. Here’s an example:
Hello, World!michaela@michaela-HP-Laptop-17-by4xxx: $
I tried researching, but I can’t find a good solution. I’m on Ubuntu 20.04, and using the BASH shell. I will provide the code if it’s any help, though I doubt it as it literally just outputs "Hello, World!".
#include <iostream>
int main(){
std::cout << "Hello, World!";
return 0;
}
2
Answers
try this :
or you can use this one :
The only difference between
std::endl
andn
is thatstd::endl
addsstd::flush
(to flush the output stream), which is usually totally unnecessary and makes the program slower.This is completely normal for the majority of programming languages, especially in a Unix environment. Even shell scripting will print without a newline if you use
printf
orecho -n
. And thanks to the terminal emulating an old teletype-like printer-based system the cursor won’t reset just because a program exitted.If you want a newline add a
'n'
to your string or output astd::endl
after your text.