0

Is there a way in C++ to get to the next line without CR (carriage return), essentially moving a line down.

std::cout << "Hello\nworld!"

output should be something like this

Hello
     world!

I also tried form feed but it prints Hello♀world!

EDIT-

Looks like I'm not very clear, so let me explain a bit more.

Windows a long time ago used CRLF for new line i.e \r\n. Here \r is meant to move cursor to far left of screen while \n moves cursor a line down. However, \n when printed brings cursor to left and a line down. I just want the initial role of \n back which just moving a line down at same column...

phuclv
  • 37,963
  • 15
  • 156
  • 475
vector X
  • 89
  • 7
  • Line feed and new line are the same thing. Yes, just use `'\n'` for your line ending instead of `"\r\n"` (carriage-return line feed - DOS/widows) or just `'\r'` (mac pre-OSX) Don't forget the one at the end -- `"Hello\nworld!\n"` – David C. Rankin Jan 31 '22 at 07:09
  • 2
    \n has been repurposed for new line instead CRLF, That is what what I know so far. I just want the original role of \n (LF). – vector X Jan 31 '22 at 07:14
  • `\n` has always been “newline”; no repurposing involved. It does whatever is appropriate to start a new line on the target system. If you don’t want those smarts, open your file in `binary` mode. That turns off runtime character translations. Typically that affects `\n` and end-of-file. – Pete Becker Jan 31 '22 at 07:20
  • 1
    It's a historic thing.. DOS used CRLF, Unix uses the newline and Mac used the carriage return before OSX. That's the story. It's just a difference in how the differing operating systems chose to implement line-endings for their system (that caused no end of grief during the 90s) The `'\n'` is ASCII 10, the `'\r'` is ASCII 13. So the different OS writers just chose a different character (or combo) for the newline.. The fact we are talking about a "carriage-return" (typewriter part) explains where the name and behavior originated. Remember a CRLF is both `"\r\n"` -- Unix just wanted one char. – David C. Rankin Jan 31 '22 at 07:21
  • 2
    @PeteBecker Old hardcopy typewriter-style terminals moved just the line on LF without moving the caret, and moved the caret to the beginning of line on CR without moving the line, hence the CR LF combination. Doing both things on just LF is a recent invention. – n. m. could be an AI Jan 31 '22 at 07:44
  • If you want to show something on a terminal, use a terminal-specific library such as ncurses. Writing `\n` to a text stream results in whatever the system needs to start a new line on the attached device. Writing `\n` to a binary stream results in writing the literal `\n` character and how each display device interprets this character is up to the device. – n. m. could be an AI Jan 31 '22 at 08:00
  • @n.1.8e9-where's-my-sharem. — in the **programming language C** (and, by extension, C++), the character sequence `\n` has always been “newline” and the runtime library for the **programming language C** has done whatever was appropriate to start a new line. As far as I know, terminal’s didn’t give any special meaning to the character ‘\’ followed by the character ‘n’. – Pete Becker Jan 31 '22 at 08:00
  • @PeteBecker There used to be a world before C. – n. m. could be an AI Jan 31 '22 at 08:06
  • @n.1.8e9-where's-my-sharem. — sigh. The question is tagged C++, not historical-mashup. And, again, I’m not aware of any hardware that gave a special meaning to a backslash followed by an n; that’s a programming-language thing, not a hardware thing. – Pete Becker Jan 31 '22 at 08:18
  • @PeteBecker OP is very clearly not talking about hardware that interprets backslashes, and neither do I. – n. m. could be an AI Jan 31 '22 at 09:24
  • Does this answer your question? [Escape sequence for a true LineFeed (LF)](https://stackoverflow.com/questions/39255198/escape-sequence-for-a-true-linefeed-lf) – JHBonarius Jan 31 '22 at 12:14
  • @n.1.8e9-where's-my-sharem. -- you keep missing the point. C and its descendants use ASCII NL, 0x10, to mark the start of a new line, and the runtime library does whatever is necessary to convince the hardware to do that. **One way** of getting ASCII NL into a character sequence is to use the escape sequence `'\n'`, but that's not the only way. What matters is the **value**, not how it's written. This is a common beginner's mistake, conflating character representations in source code with the values that end up in the executable. The title of the question gets this part exactly right. – Pete Becker Jan 31 '22 at 14:22
  • @PeteBecker I'm sorry but I have no idea what we are talking about and why. I just made a simple remark about the behaviour of ASCII NL without ever mentioning C or C++ or having intention to talk about them. – n. m. could be an AI Jan 31 '22 at 14:49

2 Answers2

3

The correct solution would be moving the cursor manually, for example with ANSI sequences if on a capable terminal. Or if you can be sure that the next line is blank or begins with all spaces then just echo the spaces manually as Jeff said:

std::cout << "Hello\n     world!";

In case you're using a *nix terminal then you can control the LF → CRLF translation with stty:

user@HOSTNAME:~$ echo -e "Hello\nworld"
Hello
world
user@HOSTNAME:~$ stty -opost # Disable LF → CRLF translation with opost mode
user@HOSTNAME:~$ echo -e "Hello\nworld"
Hello
     world
          user@HOSTNAME:~$ stty opost
user@HOSTNAME:~$ stty -onlcr # Disable LF → CRLF translation with onlcr mode
user@HOSTNAME:~$ echo -e "Hello\nworld"
Hello
     world
          user@HOSTNAME:~$
user@HOSTNAME:~$ stty raw    # Disable LF → CRLF translation with raw mode
user@HOSTNAME:~$ echo -e "Hello\nworld"
Hello
     world
          user@HOSTNAME:~$ stty cooked # Reset from raw mode to cooked mode

See also

phuclv
  • 37,963
  • 15
  • 156
  • 475
1

It sounds like you want the output cursor to go to the next line, but stay in the same column, like this:

Hello
     world!

Unfortunately, most terminals I have used will interpret a newline \n character as a combination of a newline and a carriage return. Therefore, your program must print a newline, and then enough spaces to reach the column you want:

std::cout << "Hello\n     world!";
Jeff Booth
  • 461
  • 2
  • 5