First, the most important method of use cin>>
Receives a number, character, string. "Blank", "tab", "Enter" all end
Like what:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace Std;main () {int a,b;cin>>a>>b;cout<<a+b<<endl;} </span></span>
input: 5[return]6[carriage return]
Output: 11
(The carriage return can be replaced with a space or TAB key)
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace Std;main () {char a[20];cin>>a;cout<<a<<endl;} </span></span>
Input: KLDJFLLSKJF
Output: KLDJFLLSKJF
Input: JKLJKL JKLJKL//In the event of a space end
Output: JKLJKL
Second, cin.get ()
Use method 1:cin.get (character variable name) to receive characters
Ch=cin.get (); or cin.get (CH); (Two forms equivalent)
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace Std;main () {char ch;ch=cin.get ();//or Cin.get (CH);cout<<ch< <endl;} </span></span>
Input: HGLSSF
Output: H
Use method 2:cin.get (character array name, number of characters received) to receive a line of string, to receive a space, enter to end the input
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace Std;main () {char a[20];cin.get (a,20); Cout<<a<<endl;} </span></span>
input: JKL jkl JKL
Output: JKL jkl JKL
Input: ABCDEABCDEABCDEABCDEABCDE (input 25 characters)
Output: ABCDEABCDEABCDEABCD (receive 19 characters + 1 ' s)
Use method 3:cin.get (character array name, receive character number, Terminator)
Reads a line of string, stops reading in case of Terminator
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace Std;main () {char a[20];cin.get (a,20, ' G ');cout<<a<<endl; return 0;} </span></span>
Input: Kjhkjlhgjlajdflj
Output: KJHKJLH
Note: When the Terminator is the "ENTER" key. That is, the use of the above Method 2
However, the Terminator is not discarded in either case:
Like what:
Receive character condition:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace std;int main () {char C1, C2; Cin.get (C1); Cin.get (C2); cout<<c1<< "" <<c2<<endl; Print two characters cout<< (int) c1<< "" << (int) c2<<endl; Print the ASCII value of both characters return 0;} </span></span>
Input:
A[enter]
Output:
A
97 10
"Analysis" will find that only one run from the keyboard input, obviously the first character variable taken ' A ', the second variable takes the Enter (ASCII value is ten) , which is because the function does not discard the last input at the end of the Enter character, so At the end of the first input, the buffer remains at the end of the last input. Enter characters.
Receive string condition:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #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;} </span></span>
Input:
12345[enter]
Output:
1234
53
"analysis" the first input is very long, the string is taken by length "1234" , and "5′ still remains in the buffer. So the second input character is not read from the keyboard, but instead directly takes the "5′ . So the printed ascii 53 (' 5′ ascii Value ) .
Input
12d45[enter]
Output:
12
D
"Analysis" the second output is D , indicating Terminator in buffer is not discarded when you define a terminator
third, the use of Getline () method
use Method 1:cin.getline ( array name, length, Terminator ) broadly with cin.get ( array name. Length. Terminator ) is similar.
The difference is:
cin.get () when the input string is very long, it does not cause Cin The error of the function, the following Cin The operation continues to run, simply fetching data directly from the buffer. But cin.getline () when the input is very long. can cause errors in the CIN function. Subsequent CIN Operations will no longer run.
#include <iostream>usingnamespace std;int Main () {char ch, a[20];cin.getline (A, 5); cin>>ch;cout<<a <<endl;cout<< (int) ch<<endl;return0;}
Input:
12345[enter]
Output:
1234
-52
"Analysis" andcin.get ()The sample comparison will find that theCHdoes not read the buffer in the5, but returned the-52. Here in factCin>>chstatement is not running. is due toCinsomething went wrong.
Using the method 2:getline (Cin,line), receive a line of string input and deposit into the string, press ENTER to end.
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" > #include <iostream> #include <string>using namespace Std;int main () { string line; Getline (cin,line); cout<<line<<endl; return 0; }</span></span>
input: Liudandage Nihaoa
Output: Liudandage Nihaoa
Basic methods of using CIN in C + +