Thinking about the input of C + + string

Source: Internet
Author: User

The input of a string is a key to learning C + + and a very detailed point of knowledge, and if you don't know the details, you might make mistakes while writing a program and you're overwhelmed.

At the same time, we want to understand the concept of C + + buffers, the input of the program has a buffer. The input process is like this, at the end of the input, the keyboard will be stored in the buffer data, and the corresponding reading function is read from the buffer, we can use the specific code to demonstrate how the buffer function;

Looking closely at the code above, my code defines a string of four char types and has cin input, but when we enter a string with a space, the output is direct and ignores the human continuation input, what is the reason?

Now carefully consider the concept of the input stream, when we enter the first string, I personally understand that CIN does not work at this time, we just simply put the characters (including spaces, TAB, newline) into the input stream, when we press ENTER (line feed) to stop the keyboard to feed the input stream data, The CIN input function then starts fetching data from the input stream.

For CIN, it reads directly from the buffer, when it encounters a space, TAB, newline, end input, as shown in the code above, our cin>>a extracts data from the input stream, hereisa after extracting it, encounters a space, extracts the end, and then cin>> B,cin>>c,cin>>d the same operation. So we see the above output.


Cin.get () and Cin.getline () two functions and similar, in most cases we can mix them, their input can enter a space, TAB, but when the space is encountered to end the input, their difference is getline reject the line break, and Cin.get () preserves line breaks. Let's look at the test and results of a piece of code:

Through the above two pieces of code, we see, for Cin.get (), he is reserved space, that is, he will keep the space, and it has to stop the space character, and then the remaining space is read by the next read function, equivalent to the next function to extract data directly encountered newline character , and Cin.getline () is different, it is unique, it is handsome, it comes to the end of a newline character, it can be seen as a werewolf killed the hunter, it died, it will take away a, and then it took a space, it took the space, the following input is not affected.

These C + + aspects of the input and output, should be said to be the most basic knowledge, but the basic skills are not solid, how can write robust stick code it?

Thinking about the input of C + + string

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.