Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cin's input buffer

0 views
Skip to first unread message

aegis

unread,
Aug 30, 2009, 12:32:23 AM8/30/09
to
Consider the following:

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
int a, b, c;
cin >> a; cin >> b; cin >> c;
string s;
getline(cin, s);
return 0;
}

is there a newline hanging around cin's input buffer
for getline to accept and return immediately?

Because that is how the programming is behaving.

Alf P. Steinbach

unread,
Aug 30, 2009, 1:06:40 AM8/30/09
to
* aegis:

> Consider the following:
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main(void)
> {
> int a, b, c;
> cin >> a; cin >> b; cin >> c;
> string s;
> getline(cin, s);
> return 0;
> }
>
> is there a newline hanging around cin's input buffer
> for getline to accept and return immediately?

Yes.

However, 'getline' consumes a newline.


Cheers & hth.,

- Alf

Juha Nieminen

unread,
Aug 30, 2009, 3:14:26 AM8/30/09
to
aegis wrote:
> cin >> a; cin >> b; cin >> c;

Btw, what's wrong with "cin >> a >> b >> c;"?

James Kanze

unread,
Aug 30, 2009, 7:35:01 AM8/30/09
to
On Aug 30, 6:32 am, aegis <ae...@mad.scientist.com> wrote:
> Consider the following:

> #include <iostream>
> #include <string>

> using namespace std;

> int main(void)
> {
> int a, b, c;
> cin >> a; cin >> b; cin >> c;
> string s;
> getline(cin, s);
> return 0;
> }

> is there a newline hanging around cin's input buffer
> for getline to accept and return immediately?

Probably. If you're reading from a console, most systems won't
pass the program anything until there is a new line, so the code
can't read the value for c until a new line is entered following
it. And of course, reading an int doesn't remove that new line.

The function getline doesn't necessarily look for a new line of
text; it can't know that you've already read characters from the
current line, so it simply reads from where you are up to (and
including) the next new line character.

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

aegis

unread,
Aug 30, 2009, 8:01:55 AM8/30/09
to
On Aug 30, 6:35 am, James Kanze <james.ka...@gmail.com> wrote:
> On Aug 30, 6:32 am, aegis <ae...@mad.scientist.com> wrote:
>
> > Consider the following:
> > #include <iostream>
> > #include <string>
> > using namespace std;
> > int main(void)
> > {
> >   int a, b, c;
> >   cin >> a; cin >> b; cin >> c;
> >   string s;
> >   getline(cin, s);
> >   return 0;
> > }
> > is there a newline hanging around cin's input buffer
> > for getline to accept and return immediately?
>
> Probably.  If you're reading from a console, most systems won't
> pass the program anything until there is a new line, so the code
> can't read the value for c until a new line is entered following
> it.  And of course, reading an int doesn't remove that new line.
>
> The function getline doesn't necessarily look for a new line of
> text; it can't know that you've already read characters from the
> current line, so it simply reads from where you are up to (and
> including) the next new line character.
>

Is there a preferred/standard way of discarding cin's input buffer?

aegis

unread,
Aug 30, 2009, 8:06:41 AM8/30/09
to

Seems I want cin.ignore(). Thanks for the responses!

0 new messages