Cin, Cin. Get (), Cin. Getline (), Getline (), gets (), etc.

Source: Internet
Author: User
A few C ++ input functions are always unclear. Let's sum up!

1. Cin

1. Cin. Get ()

2. Cin. Getline ()

3. Getline ()

4. Gets ()

5. getchar ()

  

1. Cin>

  

Usage 1: the most basic and common usage. Enter a number:

  

# Include

Using namespace STD;

Main ()

{

Int A, B;

Cin> A> B;

Cout <

Using namespace STD;

Main ()

{

Char A [20];

Cin>;

Cout <

Using namespace STD;

Main ()

{

Char ch;

Ch = cin. Get (); // or CIN. Get (CH );

Cout <

Using namespace STD;

Main ()

{

Char A [20];

Cin. Get (A, 20 );

Cout <

Using namespace STD;

Main ()

{

Char M [20];

Cin. Getline (M, 5 );

Cout <

# Include

Using namespace STD;

  

Main ()

{

Char M [3] [20];

For (INT I = 0; I <3; I ++)

{

For cout <"/N, enter the" <"string:" <"output M [" <"] value:" <"

  

# Include

# Include

Using namespace STD;

Main ()

{

String STR;

Getline (CIN, STR );

Cout <"

  

# Include

# Include

Using namespace STD;

Main ()

{

Char M [20];

Gets (m); // cannot be written as M = gets ();

Cout <

# Include

Using namespace STD;

  

Main ()

{

Char M [3] [20];

For (INT I = 0; I <3; I ++)

{

For cout <"/N, enter the value of" <"string:" <"output M [" <: "<> it can also be used because no space is entered here. If a space is entered, for example, "Ks KR jkl [Press enter]", CIN will have received three strings: "Ks, KR, jkl "; for example, if "kskr 1 [Press enter] kskr 2 [Press enter]", then "kskr, 1, kskr" is received. This is not the result we want! Cin. Getline () and gets () do not generate this error because they can receive spaces;

  

6. getchar () // accepts one character and must contain "# include"

  

# Include

# Include

Using namespace STD;

Main ()

{

Char ch;

Ch = getchar (); // cannot be written as getchar (CH );
Share: 
  • Previous Article: C/C ++
    Stack and stack differences [reprinted]
  • Next article: Win32 command line application, + readconsoleinput () to get the keyboard vk_code
  • A few C ++ input functions are always unclear. Let's sum up!

    1. Cin

    1. Cin. Get ()

    2. Cin. Getline ()

    3. Getline ()

    4. Gets ()

    5. getchar ()

      

    1. Cin>

      

    Usage 1: the most basic and common usage. Enter a number:

      

    # Include

    Using namespace STD;

    Main ()

    {

    Int A, B;

    Cin> A> B;

    Cout <

    Using namespace STD;

    Main ()

    {

    Char A [20];

    Cin>;

    Cout <

    Using namespace STD;

    Main ()

    {

    Char ch;

    Ch = cin. Get (); // or CIN. Get (CH );

    Cout <

    Using namespace STD;

    Main ()

    {

    Char A [20];

    Cin. Get (A, 20 );

    Cout <

    Using namespace STD;

    Main ()

    {

    Char M [20];

    Cin. Getline (M, 5 );

    Cout <

    # Include

    Using namespace STD;

      

    Main ()

    {

    Char M [3] [20];

    For (INT I = 0; I <3; I ++)

    {

    For cout <"/N, enter the" <"string:" <"output M [" <"] value:" <"

      

    # Include

    # Include

    Using namespace STD;

    Main ()

    {

    String STR;

    Getline (CIN, STR );

    Cout <"

      

    # Include

    # Include

    Using namespace STD;

    Main ()

    {

    Char M [20];

    Gets (m); // cannot be written as M = gets ();

    Cout <

    # Include

    Using namespace STD;

      

    Main ()

    {

    Char M [3] [20];

    For (INT I = 0; I <3; I ++)

    {

    For cout <"/N, enter the value of" <"string:" <"output M [" <: "<> it can also be used because no space is entered here. If a space is entered, for example, "Ks KR jkl [Press enter]", CIN will have received three strings: "Ks, KR, jkl "; for example, if "kskr 1 [Press enter] kskr 2 [Press enter]", then "kskr, 1, kskr" is received. This is not the result we want! Cin. Getline () and gets () do not generate this error because they can receive spaces;

      

    6. getchar () // accepts one character and must contain "# include"

      

    # Include

    # Include

    Using namespace STD;

    Main ()

    {

    Char ch;

    Ch = getchar (); // cannot be written as getchar (CH );
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.