cin, getline, delimiter, buffer and... confusion

B

bgeneto

Hi!

Please consider the following C++ code fragment: (using <iostream> and
<string> standard libraries)

string s; int i;
cin >> i;
getline (cin, s);

Correct me if I'm wrong: In this first case cin reads data from the
input buffer until it finds a delimiter, leaving the delimiter
(newline) in the input (keyboard) buffer. After that, the getline
function reads that delimiter directly from the input buffer, thus
ending the input process, without asking for another string data.
Right? Now consider this code

string s; int i;
cin >> i;
cin >> s;

Again, the first cin reads data until it finds the delimiter, leaving
the delimiter in the input buffer. So, why the second cin requires new
data from the user if it finds the delimiter in the buffer?

Exactly in what aspect cin behave different from getline()?

Thanks in advance,

Bernhard Georg Enders.
 
?

=?ISO-8859-1?Q?Reidar_=D8ksnevad?=

Hi!

Please consider the following C++ code fragment: (using <iostream> and
<string> standard libraries)

string s; int i;
cin >> i;
getline (cin, s);

Correct me if I'm wrong: In this first case cin reads data from the
input buffer until it finds a delimiter, leaving the delimiter
(newline) in the input (keyboard) buffer. After that, the getline
function reads that delimiter directly from the input buffer, thus
ending the input process, without asking for another string data.
Right? Now consider this code

string s; int i;
cin >> i;
cin >> s;

Again, the first cin reads data until it finds the delimiter, leaving
the delimiter in the input buffer. So, why the second cin requires new
data from the user if it finds the delimiter in the buffer?

Exactly in what aspect cin behave different from getline()?

Thanks in advance,

Bernhard Georg Enders.


While cin does leave a '\n' in the stream, it doesn't actually use it for
anything when you call cin again. getline(), on the other hand, sees the
newline character as regular input and finishes reading.

Reidar
 
B

bgeneto

If the newline character ends a cin input, why "it doesn't actually use
it for anything when you call cin again"? For me it seems that cin
flushes the buffer before actually reading anything from there. Thanks,

Bernhard.
 
K

Karl Heinz Buchegger

bgeneto said:
If the newline character ends a cin input, why "it doesn't actually use
it for anything when you call cin again"?

Because it is not the only way why an input may be declared as beeing
ended and the program might want to figure out, if that last input
has already reached the end of the input line or not.
 
J

Jon Bell

string s; int i;
cin >> i;
cin >> s;

Again, the first cin reads data until it finds the delimiter, leaving
the delimiter in the input buffer. So, why the second cin requires new
data from the user if it finds the delimiter in the buffer?

Exactly in what aspect cin behave different from getline()?

First, the behavior you describe is caused by the '>>' (stream extraction)
operator, not by cin. So your question should be, "in what respect does
'>>' behave different from getline()?"

The answer is that by design, '>>' skips any leading whitespace (blanks,
tabs and newlines) that it finds at the current input position, and does
not actually start reading data until it encounters a non-whitespace
character. getline() does not skip leading whitespace.
 
E

Evan

For me it seems that cin flushes the buffer before
actually reading anything from there. Thanks,

It's not flushing it, it just (as Jon said) ignores leading whitespace.
To convince youself of this, try the following code:

cout << "Input a number: ";
cin >> a;
cout << "Input another number: ";
cin >> b;
cout << "You said " << a << " and " << b << endl;

and when prompted the first time enter "4 5<enter>" (no quotes of
course). It will display the second prompt but won't wait for input,
then it will say you've typed 4 and 5. Here's the progression if you do
that:

1. Before the first cin, assume the input stream is empty.
2. You put "4 5\n" into the input stream
3. The first stream extraction (line 2) reads the 4 from the stream,
but leaves the following space. After this line, the input stream
contains " 4\n".
4. The second cin skips over the leading space and reads 5. It leaves
the trailing newline in the stream. After this line the stream contains
"\n".

You can see this somewhat in action by adding the following line after
the cin statements:

cout << "First character in stream: \'" <<
static_cast<char>(cin.peek()) << "\'\n";

This prints out the first character that is left in the stream. You
will be able to see that the first such message prints that the
character is ' ' and the second is '
' (of course meaning a newline). If you remove the static_casts you'll
see the ASCII code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top