Goto: A little summary of C + + input line string

Source: Internet
Author: User

Original link:

http://www.wutianqi.com/?p=1181

When we learn C + + programming, we generally use CIN in terms of input.
CIN uses whitespace (spaces, tabs, and line breaks) to set the bounds of a string.
This leads to strings with spaces , such as "I love C + + Struggle Paradise Forum"
Only "I" can be read, and the latter cannot be read in.
What happens then?

One. For character arrays:
Method One: Getline ()
Reads the entire row of data, using the line break entered by the ENTER key to determine the end of the input.
Calling method: Cin.getline (str, len);
The first parameter, STR, is the array name used to store the input rows, and the second parameter, Len, is the number of characters to read.

1 #include <iostream>
2 using namespace Std;
3
4 int Main ()
5 {
6 Char str[30];
7 Cin.getline (str, 30);
8 cout << str << Endl;
9 return 0;
10}

Method two: Get ()

Calling method: Cin.get (str, len);

1 #include <iostream>
2 using namespace Std;
3
4 int Main ()
5 {
6 Char str[30];
7 Cin.get (str, 30);
8 cout << str << Endl;
9 return 0;
10}

So what's the difference between the two?
Both read a line of input until the line break.
Then,getline discards the newline character, and get () retains the newline character in the input sequence .
Therefore, when you use Cin.get () to enter multiple rows of data, you can use get () to eliminate line breaks in the middle.

1 #include <iostream>
2 using namespace Std;
3
4 int Main ()
5 {
6 Char str1[30], str2[30];
7 Cin.get (STR1, 30);
8 Cin.get ();
9 Cin.get (STR2, 30);
Ten cout << "str1:" << str1 << Endl;
cout << "str2:" << str2 << Endl;
return 0;
13}

Because get (str, len) and get () are all class members of CIN, they can be combined to write:

1 #include <iostream>
2 using namespace Std;
3
4 int Main ()
5 {
6 Char str1[30], str2[30];
7 cin.get (STR1,). get (); Watch this!
8 Cin.get (STR2, 30);
9 cout << "str1:" << str1 << Endl;
Ten cout << "str2:" << str2 << Endl;
return 0;
12}

(Welcome everyone to my forum study: C + + struggle Paradise: Www.cppleyuan (dot) com)

Two. For the String class
Method One: Getline (CIN, str)

This shows that the getline here is not a class method.

1 #include <iostream>
2 #include <string>
3 using namespace Std;
4
5 int Main ()
6 {
7 string str;
8 Getline (CIN, str);
9 cout << str << Endl;
return 0;
11}

Goto: A little summary of C + + input line string

Related Article

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.