"C + +" string input

Source: Internet
Author: User

1.cin

First ① attempts

#include <iostream><string>usingnamespace  std; int Main () {    string  cww1;    CIN>>CWW1;    cout<<cww1;     return 0 ;}

Operation Result:

Reading a space terminates the read, so only the Makishima is displayed.

According to Bloguy's blog post:

Input principle:

The input of the program is built with a buffer, which is the input buffer. The first input process is this, when the input data is stored in the input buffer at the end of the keyboard input, and the CIN function takes the data directly from the input buffer. Because the CIN function takes data directly from the buffer, sometimes when there is residual data in the buffer, the CIN function directly obtains the residual data without requesting keyboard input.

First ② Experiment

#include <iostream>#include<string>using namespacestd;intMain () {stringCWW1; stringcww2; CIN>>CWW1; CIN>>cww2; cout<<"cww1="<<cww1<<Endl; cout<<"cww2="<<cww2<<Endl; return 0;}

Operation Result:

The first multi-input data remains in the buffer, and the second input is extracted directly from the buffer without requesting keyboard input.

2.getline () function

The Getline () function reads the entire line string, determines the end of the input by a carriage return, and calls the method Cin.geiline ()

First ① attempts

#include <iostream>usingnamespace  std; int Main () {    char cww1[];    Cin.getline (CWW1,);    cout<<cww1<<Endl;     return 0 ;}

Operation Result:

The input string, including spaces, will be read.

Getline () function complete use format:getline (array name, array length, Terminator)

First ② attempts

#include <iostream>usingnamespace  std; int Main () {    char cww1[];    Cin.getline (CWW1,");    cout<<cww1<<Endl;     return 0 ;}

Operation Result:

Read "will terminate."

First ③ attempts:

#include <iostream>usingnamespace  std; int Main () {    char cww1[];    Cin.getline (CWW1,ten);    cout<<cww1<<Endl;     return 0 ;}

Operation Result:

The length is n and the maximum number of output characters is n-1.

3.get ()

Line-oriented get (), similar to Getline () but differs in that the carriage return terminator is not discarded, in the format:cin.get (array name, array length, Terminator).

→ Related posts: http://www.cnblogs.com/A-Song/archive/2012/01/29/2331204.html

"C + +" string input

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.