Reference Original: Http://www.cnblogs.com/flatfoosie/archive/2010/12/22/1914055.html, another made some changes ~
1. Cin
2, Cin.get ()
3, Cin.getline ()
4, Getline ()
Attached: Cin.ignore (); Cin.get ()//skipping a character, such as the unwanted carriage return, space, and other characters
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: ">>" 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 from the input stream, or to discard the carriage return to compensate for Cin.get (character array name, number of characters received).
This can be seen in the "C + + geline problem" http://www.cnblogs.com/hhddcpp/p/4308587.html article, when the Getline (CIN,STR), the statement does not execute, but directly skip the time , you can add a sentence of Cin.get () before the getline sentence;
The primary is to receive unwanted characters from the discarded input stream, or enter.
3, Cin.getline ()//accept a string, you can receive a space and output
Usage 1, the third parameter defaults, the default encounters the ENTER key to stop the read in
#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
Usage 2: Custom end character (that is, pass in the third parameter)
As mentioned above, Cin.getline () actually has three parameters, Cin.getline (Receive string m, receive number 5, end character)
If the example of Cin.getline () is changed to Cin.getline (m,5, ' a '), when the input JLKJKLJKL output JKLJ, input jkaljkljkl, output JK
Extension: When used in multidimensional arrays, you can also use Cin.getline (m[i],20):
#include <iostream>
#include <string>
using namespace Std;
Main ()
{
Char m[3][20];
for (int i=0;i<3;i++)
{
cout<< "\ n Please enter the section" <<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, can receive the space and output, need to include "#include <string>"
Usage 1, the third parameter defaults, the default encounters the ENTER key to stop the read in
#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
Usage 2: Custom end character (that is, pass in the third parameter)
Getline () actually has three parameters, Getline (Receive string m, receive number 5, end character)
When the third argument is omitted, the system defaults to '
If the example is Getline (CIN,STR); Change to Getline (Cin,str, ' a '); when input JLKJKLJKL output JKLJ, input jkaljkljkl, output JK
Similar to Cin.getline (), but Cin.getline () belongs to the IStream stream, and Getline () is a string stream, which is a different two function
Go Simple summary of CIN, Cin.get (), Cin.getline (), getline () functions in C + +