C ++ enter a summary of a line of strings

Source: Internet
Author: User

When learning C ++ programming, we generally use CIN in terms of input.
Cin uses spaces (spaces, tabs, and line breaks) to define the bounds of strings.
This causesString with spacesFor example, "I love syc109.blog.163.com"
Only "I" can be read.
What should I do?

1. For character Arrays:
Method 1: Getline ()
Reads the entire row of data. It uses the line break entered by the Enter key to determine the end of the input.
Call method: cin. Getline (STR, Len );
The first parameter 'str' is used to store the name of the array in the input line, 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 2: Get ()

Call 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 is the difference between the two?
Both read a line of input until the line break.
Then,Getline discards the linefeed, while get () retains the linefeed in the input sequence..
Therefore, when using cin. Get () to input multiple rows of data, you can use get () to eliminate line breaks.

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 (); // clear the line break
9 cin. Get (str2, 30 );
10 cout <"str1:" <str1 <Endl;
11 cout <"str2:" <str2 <Endl;
12 Return 0;
13}

Because get (STR, Len) and get () are both members of the CIN class, 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, 30). Get (); // pay attention to this!
8 cin. Get (str2, 30 );
9 cout <"str1:" <str1 <Endl;
10 cout <"str2:" <str2 <Endl;
11 return 0;
12}

 

2. For the string class
Method 1: Getline (CIN, STR)

This indicates that Getline 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;
10 return 0;
11}

Continue adding ....

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.