C + + language cin and cout detailed __c++

Source: Internet
Author: User

Code compilation Run Environment QT creator

1.cin and cout introduction

CIN is a standard input stream object in the C + + programming language, that is, an object of the IStream class. CIN is mainly used to read data from standard input, where the standard input refers to the keyboard of the terminal. In addition, cout is the object of the stream, the object of the Ostream class, Cerr is the object of the standard error output stream, and is also the object of the Ostream class. The standard output here refers to the terminal keyboard, and the standard error output refers to the terminal screen.

In understanding the CIN function, we have to mention the standard input buffer. When we enter a string from the keyboard, we need to hit the ENTER key to send this string into the buffer. The typed enter key (\ r) is converted to a newline character \ n, which is also stored in the CIN buffer and counted as a character. For example, we hit the 123456 string on the keyboard and hit the Enter key (\ r) into the buffer, then the number of bytes in the buffer is 7 instead of 6.

CIN reading data is also obtained from the buffer, when the buffer is empty, the member function of CIN blocks the arrival of the waiting data, and once the buffer has data, it triggers the member function of CIN to read the data.

Information about the definition of stream object Cin、cout and flow operators is stored in the input output stream Library of C + +, so if you use the CIN, cout, and flow operators in your program, you must use the preprocessing command to include the head file iostream in this file and use the namespace std:

#include <iostream>

using namespace Std;

According to the syntax of C + +, all the actions that can be implemented and end with a semicolon are statements.

basic operation of 2.cin and cout

The general format for cout statements is:

cout<< expression 1<< expression 2<< expression 3...<< expression n;

The general format for a CIN statement is:

cin>> variable 1>> variable 2>> variable 3>>.....>> variable n;

cout: When defining a Stream object, the system creates a buffer in memory for staging data for the input and output stream. When the cout statement is executed, the inserted data order is stored in the output buffer until the output buffer is full or the Endl (or ' \ n ', Ends,flush) in the cout statement is reached and the existing data in the buffer is exported together, and the buffer is emptied. The data in the output stream is output from the system's default device (typically the monitor).

A cout statement can be divided into several lines. Such as:

1) cout<< "This was a simple C + +." <<endl;

2) cout<< "This is"//Note no semicolon

<< "A C + +"

<< "program."

<<endl;

3) cout<< "This is"; Have a semicolon

cout<< "A C + +";

cout<< "program."

cout<<endl;

The output is almost the same.

You cannot insert multiple output items with an Insert operator ' << ':

cout<<a,b,c; Error, cannot insert multiple items at once cout<<a+b+c; Right, this is an expression, as a

When using cout output, the user does not have to inform the computer according to what type of output, the system will automatically determine the type of output data, so that the output of the data according to the corresponding type output. If a is defined as int, B is float, and C is char, then:cout<<a<< ' <<b<< ' <<c<<endl; The output is: 4 3.14 A

CIN: Similar to cout, a CIN statement can be divided into several lines

1) cin>>a>>b>>c>>d;

2) Cin>>a//So the wording is clearer

>>b

>>c

>>d;

3) cin>>a;

cin>>b;

cin>>c;

The results you enter from the keyboard are the same: 1 enter key 4

When you enter with CIN, the system also extracts the corresponding length of bytes from the input stream according to the type of the variable 。 If any:

Char c1,c2;int a;float b;
cin>>c1>>c2>>a>>b;
You cannot use a CIN statement to enter a space character and a carriage return line break as a character to a character variable, and they will be skipped.

3.cin Common Reading method

When using CIN to read data from standard input, the usual method is cin>>,cin.get,cin.getline.

the use of 3.1cin>>

CIN can continuously read the desired data from the keyboard, using a space, enter, or newline as the separator.

#include <iostream>

USINGNAMESPACESTD;

int main ()

{

Char A;

int b;

float C;

String

cin>>a>>b>>c;

cout<<a<< "" <<b<< "" <<c<< "" <<endl;

System ("pause");

Return0;

}

In the screen one time input: a[carriage return]11[carriage return]5.56[carriage return], the program will output the following result:

Attention:
(1) cin>> is equivalent to cin.operator>> (), that is, a member function operator>> () is invoked to read the data.
(2) When cin>> reads data from a buffer,,cin>> ignores and clears the first character in the buffer if it is a space, tab, or newline, continues to read the next character, and waits if the buffer is empty. However, if the read succeeds, the delimiter behind the character is the,cin>> that remains in the buffer.
(3) Do not want to skip whitespace characters, then use NOSKIPWS flow control. Like cin>>noskipws>>input;.

#include <string> 
#include <iostream>
using namespace std;

int main ()
{
    char A;
    int b;
    float C;
    String str;
    cin>>a>>b>>c>>str;
    cout<<a<< "" <<b<< "" <<c<< "" <<str<<endl;

    string test;
    Getline (cin,test);/not blocking
    cout<< "test:" <<test<<endl;
    System ("pause");
    return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3-4-5 6 7 8 9 10 11 12 13 14 15 16-17

Input from keyboard: [Enter] [Enter] [Enter]]a[carriage return]5[carriage return]2.33[carriage return]hello[carriage return], the output result is:

The

Results show that,cin>> ignores the first line break in the buffer, taking the action of ignoring the purge and continuing to block the arrival of valid data from the wait buffer. However, getline () when reading data, not as cin>> ignore the first line break, Getline () found in the buffer of CIN is a residual newline character, do not block request keyboard input, direct read, feed the target string, and then replace the newline character with the empty characters ' \ 0 ', so the test in the program is an empty string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.