Principle of standard input: CIN and scanf

Source: Internet
Author: User

1.cin This method


1) Assume that Cin reads an integer. Will ignore line breaks and spaces on its own initiative. A file end tag was encountered. CIN >> A returns the number of false


int A;
while (Cin >> a) {
cout << a <<endl;
}
return 0;
Eg:1, 2, 3
1 23
1 A2
A 12
Note: When CIN reads the ' A ' character, it is assumed to be cin> (int); then Cin returns false at the same time reading the integer in variable a is 0. If the hypothesis is \n\r, then the self-neglect;


2) Cin reads the string. will take \ n as the string's cut character
String A;
while (Cin >> a) {
cout << A;
}
Note: Spaces will be treated as a cut-off character and will not be stored in a; In addition, we write "\ n" in the standard input without the effect of wrapping, so that it is stored in two characters, and we will store the "\ n" character with the return key of the keyboard.


3) CIN Read String char *
The read is completed, will be the initiative to convert \ n


4) Assume that the end of the file is encountered. Then it will also return false


How to handle 2.SCANF


Understanding of standard Input: By default, scanf reads characters from standard input. Then the related conversions are assumed to be spaces or line breaks, skipping directly, assuming that the formatting requirements are not met, then the scanf does not process the information in the buffer at the same time.




1) Read integer:
int a=12;
while (scanf ("%d", &a)!=eof) {
cout << a <<endl;
}
So. Suppose we enter ' a '. Then you'll be trapped in a dead loop


2) Read integer scanf ("%d,%d", &a, &b) is feasible. However, it is not possible to read the string scanf ("%s,%s")


Understanding of the input process: the input process consists of two parts. The first part requires us to send the data to the keyboard buffer. This process is buffered according to the line;
The second procedure is to read the data from the buffer to the corresponding memory space according to the format requirement;
3) Mixed input


int a=12, b=12;
Char c= ' C ';
cout << scanf ("%d%d%c", &a, &b, &c);

Suppose we enter "123 A1", then we will lose 1. Both B and C are not assigned values. In other words, scanf does not guarantee that the subsequent parameters are correctly assigned.


How to handle 3.fgets

First, both get and fgets can handle the input of strings, and all can enter a space. Except for that, fgets does not convert \ n into.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Principle of standard input: CIN and scanf

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.