Differences between CIN, Cin.get (), Cin.getline (), Cin.getchar () in C + +

Source: Internet
Author: User

①CIN>>: Unable to receive the space, tap key and the space, tap key, carriage return delimiter;

②cin.get (): Can receive a space, tap key and a carriage return as a terminator;

One: You can enter a single character

Format:

Char ch;

Ch=cin.get ();/cin.get (CH);

Two: Can input string

Format:

Cin.get (character array name, number of elements)

③getline (): Can receive a space, tap key and a carriage return as a terminator;

Format:

String str;//strings Variable

Getline (CIN,STR);

④cin.getline (): Can receive space, Tapa key and end with carriage return;

Format: cin.getline (character array name, number of elements)

Char m[20];

Cin.getline (m,20);

③ and ④ are similar, the singular getline () belongs to the string stream class, and Cin.getline () belongs to the IStream stream class, which is a different function.

5, the input of a blank line (that is, direct return) processing

Get (str,size): str will get ' \ n ' and set the CIN effect bit, close the input, but the carriage return remains in the input buffer, so if we reset cin with clear (), its next read input operation will capture ' \ nthe ';
Getline (str,size): str will get ' \ \ ' and will ' \ n ' remove, pail the actual bit, do not close the input. Therefore, for Cin.getline, a blank line is a valid input and does not affect the following input processing.

Gets () and GetChar () in the C language

①gets (character array name) is used to receive a string, which can include spaces, tap keys, and end with a carriage return;

②getchar (character variable name) is used to receive a single word upper ends with a carriage return, typically requiring two. the previous receive character, followed by a carriage return.

③putchar (character variable name or integer constant)

Differences between CIN, Cin.get (), Cin.getline (), Cin.getchar () in C + +

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.