C/C ++ input/output

Source: Internet
Author: User

C/C ++ input/output

I. Cin> when a space or line break '\ n' is encountered, the input ends.

This operator reads data based on the type of the following variable.

Enter the end condition: Enter, Space, and Tab are displayed.

Processing of Terminator: discards the terminator (Enter, Space, Tab) that ends the input in the buffer)

#include <iostream>using namespace std;int main(){    char str1[10], str2[10];    cin >> str1;    cin >> str2;    cout << str1 << endl;    cout << str2 << endl;    getchar();    return 0;}

[Analysis] When a string is read for the first time, spaces are stopped. abcd is read into str1, spaces are discarded, and the subsequent strings are given to the second string. This proves that the process of reading data from cin ends with a space and discards the space character. There is residual data in the buffer zone, and the read operation directly retrieves data from the buffer zone.

II.Cin. get (array name, length, Terminator)

The Terminator is an optional parameter. The maximum number of characters to be read is (-1). The Terminator specifies the characters to be read from the ending string. The default value is ENTER.

To read characters, use cin. get (char ch) or ch = cin. get ().

Read characters:

Enter the end condition: Enter

Processing the Terminator: Do not discard the Enter in the buffer

Cin. get () and cin. get (char ch) are used to read characters. Their usage is similar,

That is, ch = cin. get () is equivalent to cin. get (ch.

# Include <iostream> using namespace std; int main () {char c1, c2; cin. get (c1); cin. get (c2); cout <c1 <"" <c2 <endl; // print two characters of cout <(int) c1 <"" <(int) c2 <endl; // print the ASCII value of the two characters return 0 ;}

Test 1 input:

A [Enter]

Output:

A

97 10

[Analysis] only one input from the keyboard is executed. Obviously, the first character is 'A', and the second variable is Enter (ASCII value: 10 ), this is because this function does not discard the Enter character at the end of the last input, so the buffer left at the end of the first input is the Enter character at the end of the last input!

Test 2 input:

A B [Enter]

Output:

A

97 32

[Analysis] Obviously, the first character is 'A', and the second variable is Space (ASCII value: 32 ). The reason is the same as above. The Space character is not discarded.

Reading strings:

Enter the end condition: the default Enter key (therefore, spaces and Tab keys are acceptable). You can customize the end character on the third parameter.

Processing the Terminator: discard the Enter in the buffer

#include <iostream>using namespace std;int main (){  char ch, a[20];  cin.get(a, 5 , 'd');  cin>>ch;  cout<<a<<endl;  cout<<(int)ch<<endl;  return 0;}

Test 1 input:

12345 [Enter]

Output:

1234

53

[Analysis] The first input is too long, and the string is "1234" in length, while '5' remains in the buffer. Therefore, the second input character is not read from the keyboard, the printed ASCII value is 53 (the ASCII value of '5 ).

Test 2 input:

12d45 [Enter]

Output:

12

100

[Analysis] The second output is 100, indicating that the ending character in the buffer is not discarded when the user-defined Terminator is used.

 

III.Cin. getline () when the line break '\ n' or the maximum number of characters allowed is reached, the input ends:

Cin. getline (array name, length, Terminator) is similar to cin. get (array name, length, Terminator.

The difference is:

Cin. get () when the input string is too long, it will not cause the cin function error. Subsequent cin operations will continue, but will retrieve data directly from the buffer zone. However, when the input of cin. getline () is too long, the cin function will be wrong, and subsequent cin operations will not be executed.

#include <iostream>using namespace std;int main (){  char ch, a[20];  cin.getline(a, 5);  cin>>ch;  cout<<a<<endl;  cout<<(int)ch<<endl;  return 0;}

Test input:

12345 [Enter]

Output:

1234

-52

[Analysis] and cin. the get () example shows that the ch here does not read 5 in the buffer, but returns-52. In fact, the cin> ch statement is not executed because the cin has an error!

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.