entering newline character into a string

Agrudge Amicus

In these two cases I am entering \n as user input (in a string) in one and I am making \n as a part of string in the program itself (no user input):

string str1;
cin>>str1;           //case 1 - \n entered as the part of the input

string str="hello\n"; //case 2

in case 1 \n is considered as a part of the input string whereas in case 2 it is considered as newline - why?

Phles

Escape sequences are compiler-time only literals. When your compiler comes across a \ in a string, it looks for a pattern afterwards to determine the value. When you read in from the console, the input is read one character at a time. Most debuggers will show the inputted string as "hello\\n", or broken up into individual characters:

'h','e','l','l','o','\\','n' 

When you manually set the string in the code, such as string str = "hello\n", the compiler recognizes the escape sequence and treats it as the single character '\n'. This allows programmers to have shorthand for printing characters without going and printing their ASCII values. Users, on the other hand, have an enter button to conveniently add a newline, and programs are generally oriented to have a human-readable interface(i.e., if I type a '\' character I expect it to be a '\' if I have no experience with computers)

Another note about cin is that it uses newline characters other whitespace to distinguish between kinds of input. The function getline is meant for string input for getting around this, but the stream extraction is done from whitespace to whitespace so it is consistent with all data types(int,float,char,etc)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert multiline file into a string in bash with newline character?

JavaScript string newline character?

Decode base64 string without newline character

How to print out a newline character in a string in Awk

C++ - Cross-platform newline character in string

Insert a newline character every 10 characters in a string using Julia

gsubbing a string with a pattern containing a newline character in Lua

Converting string with comma delimited data and newline character to pandas dataframe

Splitting a string by newline character succeeds but removes the last character from the string

sed script replacing string "\n" in string with newline character

Ignoring the newline character while splitting the multiline string

Create a new row in the results set for each newline character in an entered string

Regex to remove newline character from string

C Shell script condition for if string contains a newline character

Finding the Newline Character "\n" in a String

Cannot print newline with newline character in a string

How to assert that a string has a newline character and, if so, remove it

parsing string for value having newline character

Here string SFTP isnt correctly parsing newline character

How to add a character "\t" into a string before the newline character "\n"

JSON.parse gives exception with newline character in string

C# - Reading NewLine characters from reading a string character by character

How to split a java string based on newline character

HTML string gets extra newline character if sent from server to client

Detecting newline in string and adding a character before it

Replacing a string with newline character with another string

Groupby and join in pandas dataframe for string column with newline character

What kind of newline character is in my multiline string literal?

Replacing a character plus newline with a string