Use C + + Flow member function put output single character
The cout and insert operator "<<" are generally used in the program to achieve output, and the cout stream has a corresponding buffer in memory. Sometimes the user also has special output requirements, such as outputting only one character. In addition to providing the member functions described above for format control, the Ostream class also provides a member function dedicated to outputting a single character put. Such as:
The result of calling this function is to display a character a on the screen. The parameter of the PUT function can be the ASCII code of a character or character (or an integer expression). Such as
Also displays the character a, since 97 is the ASCII code of the character A.
You can call the PUT function continuously in one statement. Such as:
Cout.put. Put (79). Put (' \ n ');
Displays good on the screen.
[Example] There is a string "BASIC" that requires that they be output in reverse order.
#include <iostream>
using namespace std;
int main ()
{
char *a= "BASIC";//The character pointer points to ' B ' for
(int i=4;i>=0;i--)
cout.put (* (a+i)); Output
cout.put (' \ n ') starting from the last character
; return 0;
}
The runtime prints on the screen:
In addition to outputting one character using the Cout.put function, you can output one character using the Putchar function. The Putchar function is used in the C language and is defined in the Stdio.h header file. C + + retains this function, defined in the iostream header file.
[Example] can also be implemented using the Putchar function instead.
#include <iostream>//can also be used #include <stdio.h>, while not the next line
using namespace std;
int main ()
{
char *a= "BASIC";
for (int i=4;i>=0;i--)
putchar (* (a+i));
Putchar (' \ n ');
}
The running result is the same as before.
The member function put can be invoked not only with cout stream objects, but also with other stream objects of the Ostream class.
C + + cin input Flow detailed
The standard input stream is the data that flows from the standard input device (keyboard) to the program. CIN, cout, Cerr, clog 4 stream objects are defined in header file iostream.h (see for details: Classes and objects related to C + + input and output), CIN is an input stream, cout, Cerr, and clog are output streams.
CIN is the object of the IStream class, which obtains data from the standard input device (keyboard), and the variables in the program extract data from the stream through the stream extractor ">>". The Stream extractor ">>" usually skips white space characters such as spaces, tabs, line breaks, and so on in the input stream when extracting data from the stream.
Note: After entering the data and then pressing ENTER, the row data is fed into the keyboard buffer, forming the input stream, and the extraction operator ">>" can extract the data from it. It is necessary to ensure that the data read from the stream works correctly.
For example:
If you enter from the keyboard
Variable a extracts the integer 21 from the input stream, and the extraction operation succeeds, at which point the Cin stream is in the normal state. But when the variable B is ready to extract an integer, it encounters the letter A, apparently the extraction operation fails, at which point the Cin stream is placed in an error state. The data can be extracted from the input stream only when it is in the normal state.
When an invalid character is encountered or a file terminator is encountered (not a newline character, the data in the file has been read), the input stream cin is in an error state, that is, the data cannot be extracted properly. All fetch operations to the CIN stream are terminated at this time. In the IBM PC and its compatible machines, the file terminator is represented by CTRL + Z. In Unix and Macintosh systems, the file terminator is represented by Ctrl + D. When the input stream cin is in an error state, if you test the value of CIN, you can find that its value is False (false), that is, the CIN is a value of 0. If the input stream is in the normal state, the value of CIN is true (true), i.e. the CIN is a value other than 0. The value of the CIN can be tested to determine whether the stream object is in the normal state and whether the fetch operation succeeded. Such as:
if (!CN)//Flow cin is in the out state, the data cannot be extracted
cout<< "error";
[Example] determines whether the stream object is in the normal state by testing the true value of the CIN.
#include <iostream>
using namespace std;
int main ()
{
float grade;
cout<< "Enter grade:";
while (Cin>>grade)//can read data from the Cin stream
{
if (grade>=85) cout<<grade<< "good!" <<endl;
if (grade<60) cout<<grade<< "fail!" <<endl;
cout<< "Enter grade:";
}
cout<< "the end." <<endl;
return 0;
}
The Stream extractor ">>" continuously extracts data from the stream (one floating-point number at a time), if successful, discovered computers to grade, at which point Cin is true, and if it is unsuccessful, CIN is false. If you type a file terminator, the data is complete.
The operating conditions are as follows:
Enter Grade:67↙
Enter Grade:89↙
good!
Enter Grade:56↙
fail!
Enter Grade:100↙
good!
Enter Grade: ^z↙//Type the end of the file
.
The program ends when a file terminator is encountered. If the data entered at one time is
Stream Extractor ">>" extracts 100, assigns to grade, carries on the processing of if statement. And then Encounter "/", think is invalid character, cin return 0. Loop ends, output "the end."
Running this program under different C + + systems is somewhat different in the final processing. The above is the result of running the program in GCC environment, if in VC + + environment (the author uses Win7 system) run this program, in the type CTRL + Z, you need to hit two times enter, the program only output "the end."