Scanf, gets, getchar, cin, cin. get, cin. getline, getline summary, cin. getcin. getline

Source: Internet
Author: User

Scanf, gets, getchar, cin, cin. get, cin. getline, getline summary, cin. getcin. getline

I. scanf

Scanf can be used to read numbers, characters, and strings;

Conclusion:
(1) When scanf reads a single character (% c) from the buffer, if the first character in the buffer is a space, tab, or line feed separator, scanfNoIgnore it and read and clear the characters in the buffer zone.
(2) When scanf reads a number or string (not a single character) from the buffer, if the first character in the buffer is a space, tab, or line feed delimiter, scanf ignores (that is, does not read) and clears the characters in the buffer. It continues to read the next character. If the buffer is empty, it continues to wait. However, if the read is successful, the delimiters behind the characters are left in the buffer zone, and scanf will not process them.
(3) When scanf reads a string, when it encounters a space, press enter, or the Tab key, the input ends. When you press enter, the space and tab keys will automatically add '\ 0' to the end of the string, but the press enter, space and tab keys will remain in the input buffer. (Therefore, it cannot receive strings that contain spaces, carriage returns, or tabs)
(4) When scanf () reads a string,
Syntax: scanf ("format control string", variable address list );
When receiving strings: scanf ("% s", character array name or pointer );

// Vertex (1)
# Include <iostream> # include <cstdio> using namespace std; int main () {char ch; for (int I = 0; I <3; I ++) {scanf ("% c", & ch); cout <ch <endl;} return 0;} input: a de output: ad
// Vertex (2)
# Include <iostream> # include <cstdio> using namespace std; int main () {char ch [10]; for (int I = 0; I <3; I ++) {scanf ("% s", & ch); cout <ch <endl;} return 0;} input: ssd xccxv cxcxv output: ssdxccxvcxcxv
// (3)
# Include <iostream> # include <cstdio> using namespace std; int main () {char ch [10]; for (int I = 0; I <3; I ++) {scanf ("% s", & ch); cout <ch <endl;} char sh [10]; cin. getline (sh, 10); cout <sh; return 0;} input: ahsbhj shbhdb sdhbj bsahd bmnb output: ahsbhjshbhdbsdhbj bsahd bm

2. gets
Gets is generally only used to read strings;
Conclusion:
(1) gets can receive spaces. If you press enter, the input ends;
(2) gets can accept all the characters entered before the Enter key and replace '\ N' with' \ 0 '. The enter key is not left in the input buffer.
(3) When gets () reads a string,
Syntax: gets (character array name or pointer );
(4) The variable s in the gets (s) function is a string pointer. If it is a single character pointer, there will be no errors in the compilation connection, but the memory overflow error will occur after running.
(5) The gets function can be read infinitely without judging the upper limit. End reading with a carriage return. Therefore, ensure that the buffer space is large enough to prevent overflow during read operations.

# Include <iostream> # include <cstdio> using namespace std; int main () {char ch [100]; gets (ch); cout <ch <endl; return 0;} input: njdks njkn njnjkb jkbh output: njdks njkn njnjkb jkbh

3. getchar
Getchar () reads one character (including space, carriage return, and Tab) in the input buffer sequence. Note that the getchar function can only accept one character, and the input number is also processed by character, when you enter more than one character, only the first character is received.
Conclusion:
(1) getchar is implemented by MACRO: # define getchar () getc (stdin ). Getchar has an int type return value. When the program calls getchar, the program waits for the user to press the key. The characters entered by the user are stored in the keyboard buffer until the user presses the carriage return, enter the input buffer (the carriage return character is also placed in the buffer ).
(2) After you press enter, getchar starts to read one character each time from the stdin stream. The Return Value of the getchar function is the ASCII code of the first character entered by the user. If an error occurs,-1 is returned and the characters entered by the user are displayed on the screen.
(3) If you enter more than one character before pressing enter, other characters will be retained in the input buffer, waiting for subsequent getchar calls to read. That is, subsequent getchar calls will not wait for the user to press the key, but will directly read the characters in the buffer until the characters in the buffer are read as the key.
(4) use of getchar:
Scenario 1:
For example, the first scanf () character '\ n' is left in the buffer when reading the input. Therefore, you can use getchar () to remove the carriage return to ensure that the reading is correct. (Read one character from the buffer zone, which is equivalent to clearing the buffer zone)

Scenario 2:

while(getchar()!='\n')

All the characters entered by the keyboard are stored in the buffer. Once you press enter, getchar enters the buffer to read the characters. Only the first character is returned at a time as the value of the getchar function, if there are loops or enough getchar statements, all the characters in the buffer are read in sequence until '\ n '. To eliminate the effect of '\ n', you can use getchar (); to clear it. Here, getchar (); only gets' \ n' but does not assign it to any character variable, so there is no effect, which is equivalent to clearing this character.
(6) You can use getche () or getch () to replace getchar (). The function is to read a character from the keyboard (you do not need to press Enter). Be sure to include the header file <conio. h>

# Include <iostream> # include <cstdio> using namespace std; int main () {char ch; for (int I = 0; I <4; I ++) {ch = getchar (); cout <ch;} return 0;} input: asd output: asd
// Empty line

Iv. cin
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: Do not read or clear the terminator (Enter, Space, Tab) that ends the input in the buffer)
Conclusion:
When cin> reads data (whether it is a character, number, or string) from the buffer, if the first character in the buffer is a space, tab, or line feed delimiter, cin> ignores and clears the buffer and continues to read the next character. If the buffer zone is empty, it continues to wait. However, if the read is successful, the delimiters behind the characters are left in the buffer zone, and cin> is not processed.

Sdfds // enter sdfds bhjb bjb // enter bhjbbjb
// Empty line
// Empty line

5. cin. get ()
This function has four common formats: No parameter, one parameter, two parameters, and three parameters.
That is, cin. get (), cin. get (char ch), cin. get (array_name, Arsize)
The corresponding function prototype is as follows:
Int cin. get (); // What is reading
Istream & cin. get (char & var); // What is reading?
Istream & get (char * s, streamsize n); // when reading a string, it ends with a line break by default.
Istream & get (char * s, streamsize n, char delim); // when reading a string, the third parameter can specify the Terminator.

# Include <iostream> # include <cstdio> # include <cstdlib> using namespace std; int main () {char ch; for (int I = 0; I <5; I ++) {ch = cin. get (); cout <ch;} return 0;} input: a12b
Output: a12b
// Empty line

(2) reading strings:
<1> cin. get (array_name, Arsize) is used to read strings and can accept space characters. When the input ends with Enter, the characters are read according to the length (Arsize.
<2> If the input is too long, the data is retrieved Based on the required length.

Char ch, a [20]; cin. get (a, 5); input: 12345 [Enter] Output: 1234

Analysis: the input is too long, and the string is named "1234" by length, while '5' remains in the buffer. Note: Each string has an empty character '\ 0' at the end ', consider the length.
<3> cin. get (array, 20); when reading a row, the system stops reading the line break, but does not process the line break. The line break remains in the input buffer. You can use cin. get () to read the linefeed into variable B for printing and verification. It is found that the print result is 10, that is, the ASCII VALUE OF THE linefeed. This is also the difference between cin. get () and getline. When getline reads a line of characters, '\ n' is terminated by default, and' \ n' is directly deleted from the input buffer, which does not affect the following input processing.

Char ch [10]; for (int I = 0; I <3; I ++ ){
Cin. get (ch, 10 );
Cout <ch;} input: asd123cd [Enter] Output: asd123cd // at this time, because the linefeed remains in the input buffer and is not absorbed, it is only read once (the first normal read, the linefeed '\ n' is used to read all the subsequent data, and the function stops reading the data when it encounters a linefeed)
Char ch [10]; for (int I = 0; I <3; I ++) {cin. get (ch, 10 );
Cin. get (); // You can also directly write it as cin. get (ch, 10). get (); cout <ch <endl;} input: asd123cd [Enter]
Qwe [Enter]
Cv [Enter] Output: asd123cd
Qwe
Cv
// Empty rows

<4> cin. get (str, size); when reading a row, the string can only be read into a C-style string, that is, char *; however, the C ++ getline function can read strings into C ++-style strings, that is, strings. In view of the two advantages of getline over cin. get (), we recommend that you use getline to read rows. The usage of getline is described in detail below.

Vi. cin. getline ()
Function: reads a string from the keyboard of the standard input device and ends with the specified Terminator.
There are two function prototypes:
Istream & getline (char * s, streamsize count); // ends with a line break by default
Istream & getline (char * s, streamsize count, char delim );
Conclusion:
(1) The read method of cin. getline () and cin. get (array_name, Arsize) is similar. It ends with Enter. It can accept space characters and read characters according to the length (Arsize.
(2) Difference between cin. getline and cin. get
<1> cin. getline does not leave the terminator or linefeed in the input buffer.
<2> cin. get (array_name, Arsize) when the input string is too long, it will not cause the cin function error. Subsequent cin operations will continue to be executed, 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.

Char ch [10]; for (int I = 0; I <3; I ++) {cin. getline (ch, 10); cout <ch <endl ;}
Input:
123456789qwe
Output:
123456789
//
//
// Empty lines in three rows

VII. getline ()
C ++ defines a global function getline In the std namespace. Because the parameter of this getline function uses a string, it is declared in the header file <string>.
Getline uses cin to read a row from the keyboard of the standard input device. The read operation ends when the following three conditions occur: 1) to the end of the file, 2) encounter a function separator, 3) input to the maximum extent.
The function prototype has two overload forms:
Istream & getline (istream & is, string & str );// End with a linefeed by default 
Istream & getline (istream & is, string & str, char delim );

String str; for (int I = 0; I <2; I ++) {getline (cin, str); cout <str <endl ;}
Input:
Asd145cd
Zxc47rtjk
Output:
Asd145cd
Zxc47rtjk
// Empty rows

Conclusion:
(1) note that when getline encounters an Terminator, it will read the terminator into the specified string, and then replace the Terminator with a null character ('\ 0 '). Therefore, getline is recommended for reading a line of characters from the keyboard, which is safer.
(2) similar to cin. getline (), but cin. getline () belongs to the istream stream, while getline () belongs to the string stream, which is different from the two functions.

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.