Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are escape characters not working when I read from cin?

Tags:

c++

(a)

string str = "Hello\nWorld"; 

When I print str, the output is:

Hello World 

(b)

string str; cin >> str;      //given input as Hello\nWorld 

When I print str, the output is:

Hello\nWorld 

What is the difference between (a) and (b)?

like image 226
kranti sairam Avatar asked Aug 15 '18 18:08

kranti sairam


People also ask

How do you escape special characters?

Escape CharactersUse the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped. Note: If you use braces to escape an individual character within a word, the character is escaped, but the word is broken into three tokens.

How do you add an escape character to a string in C++?

C++ assigns special meaning to the backslash within a string literal and requires it to be escaped to be read as an actual backslash: To represent a single backslash, it's necessary to place double backslashes (\\) in the source code. (Exception: Raw literals, supported by C++11, remove the need to escape characters.)

What is the action of escaping characters in JavaScript?

As mentioned in the introduction, the action of escaping characters means you write an “escape sequence” to represent specific characters. An escape sequence starts with a \. It is followed by a character or a series of numbers representing the Unicode character code. When we run the previous code, we obtain the following result:

How do you escape a string with Unicode characters?

An escape sequence starts with a \. It is followed by a character or a series of numbers representing the Unicode character code. When we run the previous code, we obtain the following result: If we take a closer look: We can write \" to insert a " character inside a string, which would be impossible otherwise.

What are the most helpful special characters in a string?

In my opinion, the most helpful special characters are: var q = '\''; (not covered yet). See preceding example. See preceding example. See preceding example. See String Escape Sequences for some more characters. Next, we explore line break. In case you did not know, Windows line breaks are different from Unix-based platforms.


2 Answers

The C++ compiler has certain rules when control characters are provided - documentation. As you can see, when you specify \n in a string literal it is replaced by the compiler with a line feed (value 0xa for ASCII). So instead of 2 symbols, \ and n, you get one symbol with binary code 0xa (I assume you use ASCII encoding), which makes the console move output to a new line when printed. When you read a string the compiler is not involved and your string has the actual symbols \ and n in it.

like image 124
Slava Avatar answered Oct 12 '22 11:10

Slava


When specified in a string literal, "\n" will be translated to the matching ascii code (0x0a on linux), and stored as-is. It will not be stored as a backslash, followed by a literal n. Escape sequences are for you convenience only, to allow string literals with embedded newlines.

On the other hand, your shell, running in the terminal, does not do such substitution: it submits a literal backslash and n, which will be printed as such.

To have a newline printed, enter a newline:

$ echo "Hello  World" | ./your-program  
like image 25
erenon Avatar answered Oct 12 '22 11:10

erenon