C + + standard input function __jquery

Source: Internet
Author: User

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

Attached: Cin.ignore (); Cin.get ()//skip a character, such as unwanted carriage return, space, etc.

1, cin>>

Usage 1: The most basic and most commonly used use, enter a number:

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

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

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

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

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

Input: JKLJKLJKL
Output: JKLJKLJKL

Input: JKLJKL JKLJKL//End of space
Output: JKLJKL

2, Cin.get ()

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

#include
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 received characters) is used to receive a line of strings, you can receive spaces

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

Input: JKL JKL JKL
Output: JKL jkl JKL

Input: ABCDEABCDEABCDEABCDEABCDE (enter 25 characters)
Output: ABCDEABCDEABCDEABCD (receives 19 characters + 1 "")

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

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

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

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

Input: JKLJKLJKL
Output: JKLJ

Accept 5 characters to M, where the last one is ' I ', so see only 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 (see that M, accept the number 5, end character)
When the third argument is omitted, the system defaults to ' the '
If the example Cin.getline () is changed to Cin.getline (m,5, ' a '), when the input JLKJKLJKL output JKLJ, input jkaljkljkl, output JK

When used in a multidimensional array, you can also use the Cin.getline (m[i],20) Usage:

#include
#include
using namespace Std;

Main ()
{
Char m[3][20];
for (int i=0;i<3;i++)
{
cout<< "\ n Please enter the" <<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, you can receive a space and output, you need to include "#include"

#include
#include
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 and is a different two function

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.