skip to Main Content

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


  1. try this :

    std::cout << "Hello, World!"<<std::endl;
    

    or you can use this one :

    std::cout << "Hello, World!n";
    

    The only difference between std::endl and n is that std::endl adds std::flush (to flush the output stream), which is usually totally unnecessary and makes the program slower.

    Login or Signup to reply.
  2. 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 or echo -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 a std::endl after your text.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search