Use of functions such as CIN, Cin.get (), Cin.getline (), Getline (), and gets () in C + +

Source: Internet
Author: User

When learning C + +, these input functions get a little confused, here to do a summary, in order to review their own, but also hope to be able to help later, if there is a mistake in the place also ask you a lot of advice (this article all programs through the VC 6.0 run)

1. Cin
2, Cin.get ()
3, Cin.getline ()
4, Getline ()
5, gets ()
6, GetChar ()

Attached: Cin.ignore (); Cin.get ()//skipping a character, such as the unwanted carriage return, space, and other characters

1, cin>>

Usage 1: Most basic, but also the most common usage, enter a number:

#include <iostream>
using namespace Std;
Main ()
{
int A, B;
cin>>a>>b;
cout<<a+b<<endl;
}

Input: 2[return]3[carriage return]
Output: 5

Note that:>> will filter out invisible characters (such as Space return, TAB, etc.)
cin>>noskipws>>input[j];//don't want to skip whitespace characters, use NOSKIPWS flow control

Usage 2: Accept a string, the "Space", "tab", "Enter" all end

#include <iostream>
using namespace Std;
Main ()
{
Char a[20];
cin>>a;
cout<<a<<endl;
}

Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKLJKL JKLJKL//In the event of a space end
Output: JKLJKL

2, Cin.get ()

Usage 1: cin.get (character variable name) can be used to receive characters

#include <iostream>
using namespace Std;
Main ()
{
Char ch;
Ch=cin.get (); or cin.get (CH);
cout<<ch<<endl;
}

Input: JLJKLJKL
Output: J

Usage 2:cin.get (character array name, number of characters received) is used to receive a line of string, can receive a space

#include <iostream>
using namespace Std;
Main ()
{
Char a[20];
Cin.get (a,20);
cout<<a<<endl;
}

Input: JKL JKL JKL
Output: JKL jkl JKL

Input: ABCDEABCDEABCDEABCDEABCDE (input 25 characters)
Output: ABCDEABCDEABCDEABCD (receive 19 characters + 1 ' s)

Usage 3:cin.get (no parameters) No arguments are used primarily to discard unwanted characters in the input stream, or to discard the carriage return to compensate for the lack of cin.get (character array name, number of characters received).

This I do not know how to use, know the elder please enlighten;

3, Cin.getline ()//accept a string, you can receive a space and output

#include <iostream>
using namespace Std;
Main ()
{
Char m[20];
Cin.getline (m,5);
cout<<m<<endl;
}

Input: JKLJKLJKL
Output: JKLJ

Accept 5 characters into M, where the last one is ' s ', so only see 4 characters output;

If you change 5 to 20:
Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKLF FJLSJF FJSDKLF
Output: JKLF fjlsjf FJSDKLF

Extended:
Cin.getline () actually has three parameters, Cin.getline (accepts the string to see Oh that M, accepts the number 5, the ending character)
When the third argument is omitted, the system defaults to '
If the example of Cin.getline () is changed to Cin.getline (m,5, ' a '), when the input JLKJKLJKL output JKLJ, input jkaljkljkl, output JK

When used in multidimensional arrays, you can also use Cin.getline (m[i],20), such as:

#include <iostream>
#include <string>
using namespace Std;

Main ()
{
Char m[3][20];
for (int i=0;i<3;i++)
{
cout<< "\ n Please enter the section" <<i+1<< "string:" <<endl;
Cin.getline (m[i],20);
}

cout<<endl;
for (int j=0;j<3;j++)
cout<< "Output m[" <<j<< "] Value:" <<m[j]<<endl;

}

Please enter a 1th string:
Kskr1

Please enter a 2nd string:
Kskr2

Please enter a 3rd string:
Kskr3

Value of output M[0]: KSKR1
Value of output M[1]: KSKR2
Value of output M[2]: KSKR3

4, Getline ()//accept a string, can receive the space and output, need to include "#include <string>"

#include <iostream>
#include <string>
using namespace Std;
Main ()
{
String str;
Getline (CIN,STR);
cout<<str<<endl;
}

Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKL JFKSLDFJ JKLSJFL
Output: JKL jfksldfj JKLSJFL

Similar to Cin.getline (), but Cin.getline () belongs to the IStream stream, and Getline () is a string stream, which is a different two function

Use of functions such as CIN, Cin.get (), Cin.getline (), Getline (), and gets () in C + +

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.