Re: why cin.getline does not wait for an input?

From: Tim Roberts (timr_at_probo.com)
Date: 12/19/04


Date: Sat, 18 Dec 2004 22:21:23 -0800


"Geoff" <Geoff@discussions.microsoft.com> wrote:
>
>Thank you very much very good, this was well worth the wait.
>
>Just out of curiosity, does the stream into cin share the memory on each
>time it's called, is it a union or something similar?

I'm not sure what you're asking. The buffer for cin is just a stream of
bytes. When you do this:

   int i;
   cin >> i;

The C++ compiler knows this is a "right shift" operator between an object
of type istream (or istreambuf or something like that) and an object of
type int. It searches for a function that takes those two parameters and
finds:

    istream& operator>> (istream&, int);

It then includes a call to that function in your code. It is that function
that knows how to read characters from the stream and convert them to an
integer. Same thing happens with a different type. If you do this:

   int i;
   char str[40];
   cin >> i >> str;

You get two calls. One to
   istream& operator>>(istream&,int);
and one to
   istream& operator>>(istream&,char*);

-- 
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc


Relevant Pages

  • Re: is cin always the keyboards input?
    ... the whole point of 'cin' is to keep this detail hidden from you. ... > input stream) as cin. ... testdatamaker outputs strings every second, ...
    (comp.lang.cpp)
  • Re: std::cin.ignore() and std::cin.clear()
    ... So what does cleardo anyway, if not clear all cin data? ... It clears the error state of the stream. ... The extracts an unlimited amount of characters (the max is a sentinel ... C++ FAQ: http://www.parashift.com/c++-faq-lite/ ...
    (comp.lang.cpp)
  • Re: is cin always the keyboards input?
    ... the whole point of 'cin' is to keep this detail hidden from you. ... >> cat file | myapp ... Set stdin to non-blocking mode ... Here the stream will be in an error state. ...
    (comp.lang.cpp)
  • Re: cin error recovery
    ... cin>> i1; ... > When stream input encounters an illegal character, ... > have to be on separate lines. ...
    (comp.lang.cpp)
  • Re: Wierd newbie problem
    ... > You should flush cout before reading from cin. ... > it's possible for the text to not actually display ... > endl writes a newline and flushes the stream. ...
    (alt.comp.lang.learn.c-cpp)