1, cin>>
Usage 1: Most basic, but also the most common usage, enter a number:
#include <iostream> using namespace std; Main () {int A, B; cin>>a>>b; cout<<a+b<<endl;}
Input: 2[return]3[carriage return]
Output: 5
Note that:>> will filter out invisible characters (such as Space return, TAB, etc.)
cin>>noskipws>>input[j];//don't want to skip whitespace characters, use NOSKIPWS flow control
Usage 2: Accept a string, the "Space", "tab", "Enter" all end
#include <iostream> using namespace std; Main () {char a[20]; cin>>a; cout<<a<<endl;}
Input: JKLJKLJKL
Output: JKLJKLJKL
Input: JKLJKL JKLJKL//In the event of a space end
Output: JKLJKL
2, Cin.get ()
Usage 1:cin.get (character variable name) can be used to receive characters
#include <iostream> using namespace std; Main () {char ch; ch=cin.get (); or cin.get (CH); cout<<ch<<endl; }
Input: JLJKLJKL
Output: J
Usage 2:cin.get (character array name, number of characters received) is used to receive a line of string, can receive a space
#include <iostream> using namespace std; Main () {char a[20]; Cin.get (a,20); cout<<a<<endl;}
Input: JKL JKL JKL
Output: JKL jkl JKL
Input: ABCDEABCDEABCDEABCDEABCDE (input 25 characters)
Output: ABCDEABCDEABCDEABCD (receive 19 characters + 1 ' s)
Usage 3:cin.get (no parameters) No arguments are used primarily to discard unwanted characters in the input stream, or to discard the carriage return to compensate for the lack of cin.get (character array name, number of characters received).
This I do not know how to use, know the elder please enlighten;
3, Cin.getline ()//accept a string, you can receive a space and output
#include <iostream> using namespace std; Main () {char m[20]; Cin.getline (m,5); cout<<m<<endl;}
Input: JKLJKLJKL
Output: JKLJ
Accept 5 characters into M, where the last one is ' s ', so only see 4 characters output;
If you change 5 to 20:
Input: JKLJKLJKL
Output: JKLJKLJKL
Input: JKLF FJLSJF FJSDKLF
Output: JKLF fjlsjf FJSDKLF
Extended:
Cin.getline () actually has three parameters, Cin.getline (accepts the string to see Oh that M, accepts the number 5, the ending character)
When the third argument is omitted, the system defaults to '
If the example of Cin.getline () is changed to Cin.getline (m,5, ' a '), when the input JLKJKLJKL output JKLJ, input jkaljkljkl, output JK
When used in multidimensional arrays, you can also use Cin.getline (m[i],20), such as:
#include <iostream> #include <string> using namespace Std;main () {char m[3][20]; for (int i=0;i<3;i++) {cou t<< "\ n Please enter the section" <<i+1<< "string:" <<endl; Cin.getline (m[i],20); }cout<<endl; for (int j=0;j<3;j++) cout<< The value of "Output m[" <<j<< "]:" <<M[J]<<ENDL;}
Please enter a 1th string:
Kskr1
Please enter a 2nd string:
Kskr2
Please enter a 3rd string:
Kskr3
Value of output M[0]: KSKR1
Value of output M[1]: KSKR2
Value of output M[2]: KSKR3
4, Getline ()//accept a string, can receive the space and output, need to include "#include <string>"
#include <iostream> #include <string> using namespace std; Main () {string str; getline (CIN,STR); cout<<str<<endl;}
Input: JKLJKLJKL
Output: JKLJKLJKL
Input: JKL JFKSLDFJ JKLSJFL
Output: JKL jfksldfj JKLSJFL
Similar to Cin.getline (), but Cin.getline () belongs to the IStream stream, and Getline () is a string stream, which is a different two function
Note: In the use of getline, there is a strange phenomenon, under the VC6, to play two times to return to the end
5, get ()//accept a string, you can receive a space and output, need to include "#include <string>"
#include <iostream> #include <string> using namespace std; Main () {char m[20]; gets (m); cannot be written as m=gets (); cout<<m<<endl; }
Input: JKLJKLJKL
Output: JKLJKLJKL
Input: JKL JKL JKL
Output: JKL jkl JKL
Like an example in Cin.getline (), get () can also be used in multidimensional arrays:
#include <iostream> #include <string> using namespace Std;main () {char m[3][20]; for (int i=0;i<3;i++) {cou t<< "\ n Please enter the section" <<i+1<< "string:" <<endl; Gets (M[i]); }cout<<endl; for (int j=0;j<3;j++) cout<< The value of "Output m[" <<j<< "]:" <<M[J]<<ENDL;}
Please enter a 1th string:
Kskr1
Please enter a 2nd string:
Kskr2
Please enter a 3rd string:
Kskr3
Value of output M[0]: KSKR1
Value of output M[1]: KSKR2
Value of output M[2]: KSKR3
The use of self-feeling gets () and Cin.getline () is very similar, but Cin.getline () more than one parameter;
Here incidentally, for this KSKR1,KSKR2,KSKR3 example, for the cin>> can also be applied, because there is no space entered here, if you enter a space, such as "KS kr jkl[carriage return", then Cin will have received 3 strings , "KS,KR,JKL", again such as "Kskr 1[return]kskr 2[carriage return", then receive "Kskr,1,kskr"; this is not the result we want! Cin.getline () and get () do not generate this error because they can receive spaces;
6, GetChar ()//accept a character, need to include "#include <string>"
#include <iostream> #include <string> using namespace std; Main () {char ch; ch=getchar ();//cannot be written as GetChar (CH); cout<<ch<<endl;}
Input: JKLJKLJKL
Output: J
GetChar () is a C language function, C + + can also be compatible, but try not to use or less;
Cin/cin.get ()/cin.getline ()/getline ()/gets ()/getchar ()