Array, String, Cin cin. Getline () cin. Get () Getline () gets ()

Source: Internet
Author: User

Array:

Float loans [20];

The loans type is not "array" but "float array ". This emphasizes that the loans array is created using the float type.

 

String:

Char dog [5] = {'A', 'B', 'C', 'D', 'E'}; // not a string (not a string)

Char dog [5] = {'A', 'B', 'C', 'D', '\ 0'}; // a string (a string)

 

Differences between character and string

Char shirt_size = 's'; In the ASCII system, 's' is only another method of 83.

However, "S" represents a string consisting of two characters (S and \ 0. And "S" actually indicates
The memory address of the string. Therefore, the following statements are available:

Char shirt_size = "S"; give the memory address to the shirt_size. Because the address is an independent type in C ++, this unreasonable approach is not allowed for C ++ compilation.

 

Cin cin. Get cin. Getline gets getchar

Cin> how to confirm that the string has been entered?

Because empty characters cannot be entered on the keyboard, CIN must have other methods to determine the end position of the string. Cin uses blank space (space, tab, and line break)

To define the bounds of strings. This means that CIN reads only one word at the given number of characters.

Cin. Get ()

Usage 1. It can be used to receive characters

Such as: cin. Get () Input agfgsdfg
Output

Usage 2. It can be used to receive a line of strings and receive Spaces

Such as: cin. Get (character array name, number of characters received (such as 20 ))

Input: abcdeabcdeabcdeabcdeabcde
(Enter 25 characters)

Output: abcdeabcdeabcdeabcd
(Receiving 19 characters + 1 '\ 0'

Cin. Getline () receives a string, can receive spaces and Output

Extension:

Cin. Getline () actually has three parameters: cin. Getline (accept the string to see the M, accept the number of 5, end character)

If the third parameter is omitted, the default value is '\ 0'

In this example, change cin. Getline () to CIN. Getline (M, 5, 'A'). When jlkjkljkl is input, jklj is output. When jkaljkljkl is input, JK is output.

The difference between cin. Get () and CIN. Getline () is that both functions read a row of input until the line breaks. Then, Getline ()

The linefeed will be discarded, while the get () will keep the linefeed in the input sequence as follows:

Char name [10];

Cout <"enter your name :";

Cin. Getline (name, 10 );

The user enters jub to respond, and then press ENTER

Enter your name: Jud

Getline () accepts a string, and can receive spaces and output. It must contain "# include <string>"

Gets () accepts a string, can receive spaces and output, must contain "# include <string>"

Getchar () accepts one character, and can receive spaces and output. It must contain "# include <string>"

Char ch;

Char = getchar ();

Cout <ch <Endl;

Input fasdfasdf

Output F

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.