Jump to content

basic C++ question

Guest Gnome Chomsky

Recommended Posts

Guest Gnome Chomsky

I'm fairly new to C++ but I have experience in C. I asked some people and they don't know so I thought I'd post the question on here. 


Is there a way to print multiple line breaks without putting cout << endl << endl << endl << endl etc etc etc? 


I know you can do cout << "\n\n\n\n\n etc etc etc" but I prefer endl over "\n". 


I tried cout << 5endl and cout << endl*5 but neither of those worked. Is it tacky stylistically to put a bunch of endl's? I like a lot of space in my programs. 

Link to comment
Share on other sites

I'm not an expert on C++. Like you, I know C and I have used a bit of C++ only. But here are my thoughts:


Having a bunch of blank spaces in your output sounds like a good idea to me. Sometimes I also use "***********" or "----------------" strings to help me see different sections.


I don't think stuff like endl*5 would work because endl is probably encoded as some unicode value or something and multiplying that value by 5 might result in something undesired?


I think your best bet, for simple code, is cout << "\n\n\n\n\n"


I'm not sure what you mean by saying you prefer endl over \n. When I looked up the difference, I see that the only difference between endl and \n (new line) is that endl will flush a buffered stream. cout is not a buffered stream, so the behaviour of endl and \n is going to be the same in this case? Unless you mean you prefer reading/writing code with "endl" instead of "\n". If so, that's fine but I would point out that "\n" is something used in many different languages while I have not seen "endl" in many other places than C++ (I'm sure it exists, but I feel that "\n" is the standard "new line" character).

Link to comment
Share on other sites

  • 2 weeks later...

C++ isn't really the language for doing text manipulation and the like. endl or "\n" is the best thing you can use, otherwise if that is a possibility for you, using regular expressions instead of C++ as they'll probably be much more efficient to do that sort of thing.


There are different schools with regards to what is better between endl or "\n", my personal *preference* is to stick to the basic STL and go with std::endl. For (elaborate) text manipulation I prefer using sed if I am on Linux/Unix.

Edited by MikKar
Link to comment
Share on other sites

#include <iostream>

using namespace std;


int main() {

      int numberOfNewLines = 5;


      for(int i = 0; i < numberOfNewLines; i++)

          cout << endl;


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

This website uses cookies to ensure you get the best experience on our website. See our Privacy Policy and Terms of Use