Turn from: https://www.cnblogs.com/luolizhi/p/5746775.html
1, cin>>
Usage 1: The most basic and most commonly used use, enter a number:
#include <iostream>
using namespace std;
Main ()
{
int a,b;
cin>>a>>b;
cout<<a+b<<endl;
}
Input: 2[carriage return]3[carriage return]
Output: 5
Note that:>> will filter out invisible characters (such as Space return, TAB, etc.)
cin>>noskipws>>input[j];//do not want to skip whitespace characters, then 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//End of space
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 received characters) is used to receive a line of strings, you can receive spaces
#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 (enter 25 characters)
Output: ABCDEABCDEABCDEABCD (receives 19 characters + 1 "")
3, Cin.getline ()//accept a string, you can receive spaces 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 to M, where the last one is ' I ', so see only 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 (see that M, accept the number 5, end character)
When the third argument is omitted, the system defaults to ' the '
If the example Cin.getline () is changed to Cin.getline (m,5, ' a '), when the input JLKJKLJKL output JKLJ, input jkaljkljkl, output JK
When used in a multidimensional array, you can also use the Cin.getline (m[i],20) Usage:
#include <iostream>
#include <string>
using namespace std;
Main ()
{
char m[3][20];
for (int i=0;i<3;i++)
{
cout<< \ n Please enter the "<<i+1<<" string: "<<endl;
Cin.getline (m[i],20);
}
cout<<endl;
for (int j=0;j<3;j++)
cout<< output m["<<j<<"] Value: "<<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, you can receive a space and output, you 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 and is a different two function.
5, gets (), GetChar (), scanf ()
#include <iostream>
using namespace std;
int main ()
{
char str1[20], str2[20];
Char cha;
Gets (STR1);
Cha = GetChar ();
scanf ("%s", &STR2);
cout << str1 << endl << cha << endl << str2 << Endl;
return 0;
}
Input:
Hello World
H
Hello World
Output:
Hello World
H
Hello