Standard input (cin) In most program environments, the standard input by default is the keyboard, and the C stream object defined to access it is cin. For formatted input operations, cin is used together with the extraction operator, which is written as (i.e., two 'greater than' signs). This operator is then followed by the variable where the extracted data is stored. For Visual C 4.2 and later versions, this behavior only occurs when using the old iostream libraries. There are four methods to flush the cout buffer, as follows: Use the endl manipulator to insert a newline character into the output stream and flush the buffer. Use the insertion operator with the endl manipulator, as follows.
< cpp | io
The global objects
std::cin and std::wcin control input from a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C input stream stdin.
These objects are guaranteed to be initialized during or before the first time an object of type std::ios_base::Init is constructed and are available for use in the constructors and destructors of static objects with ordered initialization (as long as
<iostream> is included before the object is defined).
Unless sync_with_stdio(false) has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted input.
Once
std::cin is constructed, std::cin.tie() returns &std::cout, and likewise, std::wcin.tie() returns &std::wcout. This means that any formatted input operation on std::cin forces a call to std::cout.flush() if any characters are pending for output.
[edit]Notes
The 'c' in the name refers to 'character' (stroustrup.com FAQ);
cin means 'character input' and wcin means 'wide character input'
[edit]Example
Output:
[edit]See also
Retrieved from 'https://en.cppreference.com/mwiki/index.php?title=cpp/io/cin&oldid=86052'
-->
Declares objects that control reading from and writing to the standard streams. This include is often the only header you need to do input and output from a C++ program.
Syntax
Note
The <iostream> library uses the
#include <ios> , #include <streambuf> , #include <istream> , and #include <ostream> statements.
Remarks
The objects fall into two groups:
Once you do certain operations on a stream, such as the standard input, you can't do operations of a different orientation on the same stream. Therefore, a program can't operate interchangeably on both cin and wcin, for example.
All the objects declared in this header share a peculiar property — you can assume they're constructed before any static objects you define, in a translation unit that includes <iostream>. Equally, you can assume that these objects aren't destroyed before the destructors for any such static objects you define. (The output streams are, however, flushed during program termination.) Therefore, you can safely read from or write to the standard streams before program startup and after program termination.
This guarantee isn't universal, however. A static constructor may call a function in another translation unit. The called function can't assume that the objects declared in this header have been constructed, given the uncertain order in which translation units participate in static construction. To use these objects in such a context, you must first construct an object of class ios_base::Init.
Global Stream Objects
cerr
The object
cerr controls output to a stream buffer associated with the object stderr , declared in <cstdio>.
Return Value
An ostream object.
Remarks
The object controls unbuffered insertions to the standard error output as a byte stream. Once the object is constructed, the expression
cerr. flags& unitbuf is nonzero, and cerr.tie() &cout .
Examplecin
Specifies the
cin global stream.
Return Value
An istream object.
Remarks
The object controls extractions from the standard input as a byte stream. Once the object is constructed, the call
cin. tie returns & Recording headset with auto tune microphone. cout.
Example
In this example,
cin sets the fail bit on the stream when it comes across non-numeric characters. The program clears the fail bit and strips the invalid character from the stream to continue.
clog
Specifies the
clog global stream.
Return Value
An ostream object.
Remarks
The object controls buffered insertions to the standard error output as a byte stream.
Example
See cerr for an example of using
clog .
cout
Specifies the
cout global stream.
Return Value
An ostream object.
Remarks
The object controls insertions to the standard output as a byte stream.
Example
See cerr for an example of using
cout .
wcerr
Specifies the
wcerr global stream.
Return Value
A wostream object.
Remarks
The object controls unbuffered insertions to the standard error output as a wide stream. Once the object is constructed, the expression
wcerr. flags& unitbuf is nonzero.
Example
See cerr for an example of using
wcerr .
wcin
Specifies the
wcin global stream.
Return Value
A wistream object.
Remarks
The object controls extractions from the standard input as a wide stream. Once the object is constructed, the call
wcin. tie returns & wcout.
Example
See cerr for an example of using
wcin .
wclog
Specifies the
wclog global stream.
Return Value
A wostream object.
Remarks
The object controls buffered insertions to the standard error output as a wide stream.
Example
See cerr for an example of using
wclog .
wcout
Specifies the
wcout global stream.
How To Use Cout And Cin In Dev C 5Return Value
A wostream object.
Remarks
The object controls insertions to the standard output as a wide stream.
How To Use Cout In Dev C++ExampleHow To Use Cout
See cerr for an example of using
wcout .
CString instances in a wcout statement must be cast to const wchar_t* , as shown in the following example.
For more information, see Basic CString Operations.
Using Cout And Cin In C++See alsoCout And Cin
Header Files Reference
Thread Safety in the C++ Standard Library iostream Programming iostreams Conventions Comments are closed.
|