>" extracts the contents of cin and stored" /> >" extracts the contents of cin and stored" /> >" extracts the contents of cin and stored"/>

What is the name of the data member of an object "cin" which stores an input data in C++?

109 Views Asked by At

Eg. cin>>var; Object cin reads input data from the user and as soon as the ENTER key is hit, the operator ">>" extracts the contents of cin and stored into the specific variable "var". That means the input data were first stored in the cin object.

Now, if cin stores the data, then I'm curios to know to which MEMBER DATA of the cin object will the inputted data will be stored??? Eg. cin.memberdataname or cin.xxxx etc?? What is the name of the member data where the inputted data are first initially stored?

3

There are 3 best solutions below

2
Molnár Levente On

if cin stores the data

No, it does not store the data. It links the standard input stream to the memory address. Standard input does not have to be a keyboard, since a txt file can serve as input, but cin stands for console input and what it really does is to grab your OS' console interface which is tied to the keyboard and ties the app on the chain. A way to test this is to check the assembly code of your app, you'll see only one variable, which is "var". However, there are other ways to get the input than using cin, but that is for an other question, if you are that curios.

0
Miles Budnek On

It's left entirely unspecified by the C++ language how std::cin (or rather, it's associated std::streambuf implementation) reads data from the standard input stream. The only thing C++ specifies is that it does so (and that, by default, that reading is synchronized with the C stdio functions like scanf, fread, and friends).

The common approach is for std::cin's std::streambuf to internally use the C stdio functions and provide minimal or no buffering in between. For example, if you look at the libstdc++ implementation, you can see that it doesn't use the std::streambuf's buffer pointers at all and just directly returns the result of fread, getc, and similar C I/O functions.

Going one step deeper, the FILE structure pointed to by the FILE* that std::cin's std::streambuf contains may very well buffer data it reads from the underlying OS I/O functions (i.e. POSIX read, Windows ReadFileEx, etc), but the FILE structure is opaque to standard library users. Its definition is not exposed, so you cannot inspect it except through the C stdio functions.

0
Jean-Baptiste Yunès On

cin>>var is a call to a function like operator>>(cin,var) of prototype istream &operator>>(istream &s,sometype & var). var is passed by reference. When the input is decoded the result is just stored into var.

For example, you can overload that operator for your own to extract a Date with three fields month, day and year:

istream &operator>> (istream &is, Date &dt) {
    is >> dt.month >> dt.day >> dt.year;
    return is;
}

Nothing very different for standard types, just decoding and storing into the referenced variable.