C++ based on cin cin. Get of cin. Getline of getline of gets of

  • 2020-04-01 23:27:44
  • OfStack

1, cin
2, cin. The get ()
3, cin. Getline ()
4, getline ()
5, gets ()
6, getchar ()

Add: cin. Ignore ();   Cin. Get ()// skips a character, such as an unwanted carriage return, space, etc

1, cin > >                  

Usage 1: the most basic and common usage, enter a number:

# include < iostream >
Using namespace STD.
The main ()
{
    Int a, b;
    cin > > a. > > B;
    cout < < A + b < < Endl;
}

Input: 2[enter]3[enter]
Output: 5

Note: > > Will filter out invisible characters (such as space carriage return, TAB, etc.)
cin > > noskipws > > Input [j]; // to avoid skipping white space characters, use noskipws flow control

Usage 2: accept a string and end with "space", "TAB", and "enter"

# include < iostream >
Using namespace STD.
The main ()
{
    Char a, [20].
    cin > > A;
    cout < < a. < < Endl;
}

Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKLJKL JKLJKL             // ends with a space
Output: JKLJKL

2, cin. The get ()

Usage 1: cin. Get can be used to receive characters

# include < iostream >
Using namespace STD.
The main ()
{
    Char ch;
    Ch = cin. The get ();                             / / or cin. Get (ch);
    cout < < ch < < Endl;
}

Input: JLJKLJKL
Output: j

Usage 2: cin. Get is used to receive a line of strings. Spaces can be received

# include < iostream >
Using namespace STD.
The main ()
{
    Char a, [20].
    Cin. Get (a, 20);
    cout < < a. < < Endl;
}

Input: JKL JKL JKL JKL
Output: JKL JKL JKL JKL

Input: abcdeabcdeabcdeabcdeabcde (input 25 characters)
Output: abcdeabcdeabcdeabcd                           (receive 19 characters +1 '\0')

No parameters are mainly used to discard the unnecessary characters in the input stream, or to discard the carriage return, to make up for the shortage of cin. Get (because cin. Get () keeps the newline character in the input buffer, cin. Get () can be used to discard the newline character.


3, cin. Getline ()     // accepts a string, can receive Spaces and output

# include < iostream >
Using namespace STD.
The main ()
{
    Char m [20];
    Cin. Getline (m, 5);
    cout < < m < < Endl;
}

Input: JKLJKLJKL
Output: JKLJ

Accept 5 characters into m, the last of which is '\0', so you only see 4 characters output;

If I change 5 to 20:
Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKLF FJLSJF FJSDKLF
Output: JKLF FJLSJF FJSDKLF

/ / extension:
//cin. Getline () actually has three parameters, cin. Getline (accepts the storage space m of the string, accepts the number 5, and ends the character)
// when the third parameter is omitted, the system defaults to '\n'
// if cin. Getline () in the example is changed to cin. Getline (m,5,'\n'); When input JLKJKLJKL, output JKLJ; when input jkaljkljkl, output jk; at this time, the status flag bit false of cin (as long as the number of input exceeds the number of accepted is not considered to be the correct end, if the function getline() is used, need cin. Clear ())

When used in multidimensional arrays, I can also use cin. Getline (m[I],20) or something like that:

# include < iostream >
# include < The string >
Using namespace STD.

The main ()
{
    Char m [3] [20];
    For (int I = 0; i. < 3; I++)
    {
          cout < < "\n please type" < < I + 1 < < "String:" < < Endl;
          Cin. Getline (m [I], 20);
    }

    cout < < Endl;
    For (int j = 0; j < 3; J++)
    cout < < "[" output m < < j < < The value of "] :" < < M [j]. < < Endl;

}

Please enter the first string:
kskr1

Please enter the second string:
kskr2

Please enter the third string:
kskr3

Output m[0] value :kskr1
Output value of m[1] :kskr2
Output m[2] value :kskr3

4, getline ()         // accepts a string, accepts a space and outputs it, including "#include < The string > "

# include < iostream >
# include < The string >
Using namespace STD.
The main ()
{
      String STR.
      Getline (cin, STR);
      cout < < STR < < Endl;
}

Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKL JFKSLDFJ JKLSJFL
Output: JKL JFKSLDFJ JKLSJFL

It is similar to cin. Getline (), but cin. Getline () belongs to the istream stream, while getline() belongs to the string stream and is a different function


Related articles: