Reputation: 2127
I am a beginner for C++ so I'm sorry if this question sounds stupid..
I made this little program to help me get familiar with the properties of cin
:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string next;
cout<<"Enter your input.\n";
cin>>next;
cout<<next;
return 0;
}
When I typed in \n
from the keyboard input, I was returned \n
.
Also, when I changed the variable next from a string to a character and gave it the same input as above, I was returned only a \
.
My question is: Why am I not returned with a new line instead? Doesn't cin
recognize \n
type in from keyboard as a newline character? Or is it just applicable to cout
?
Upvotes: 3
Views: 11202
Reputation: 153919
\n
is an escape sequence in C++; when it appears in a character constant or a string literal, the two character sequence is replaced by the single character representing a new line in the default basic encoding (almost always 0x0A in modern systems). C++ defines a number of such escape sequences, all starting with a \
.
Input is mapped differently, and in many cases, depending on the device. When reading from the keyboard, most systems will buffer a full line, and only return characters from it when the Enter key has been pressed; what he Enter key sends to a C++ program may vary, and whether the file has been opened in text mode or binary mode can make a difference as well—in text mode, the C++ library should negotiate with the OS to ensure that the enter key always results in the single character represented by \n
. (std::cin
is always opened in text mode.) Whether the keyboard driver does something special with \
or not depends on the driver, but most don't. C++ never does anything special with \
when inputting from a keyboard (and \n
has no special meaning in C++ source code outside of string literals and character constants).
Upvotes: 2
Reputation: 3870
Technically speaking, this depends on things outside your program, but assuming your terminal simply passes the individual bytes corresponding to the '\' and 'n' characters (which I think any sane one will), then the behavior you're seeing is expected.
"\n" is nothing more than a shortcut added to the programming language and environment to let you more easily represent the notion of the ASCII return key. It's not a character itself, just a command to tell the program to generate a non-printable character that corresponds to pressing the Enter key.
Let's say you're in Notepad or whatever and you press the Tab key. It tabs over a spot. Typing "\t" just enters the literal characters "\" and "t". Internally, whoever wrote Notepad had to say what it should do when the user pressed Tab, and he did so by using the mnemonic like
if(key == '\t') {
// tab over
}
Upvotes: 2
Reputation: 1960
If you need your program to recognize \n
as a new line character at input you can check this out:
https://stackoverflow.com/a/2508814/815812
What Michael say is perfectly correct.
You can try out in similar way.
Upvotes: 2