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