Character input for C + +

Source: Internet
Author: User

The input of the string has 6 in the way, these six ways each have each characteristic, I this study note is own experience summary, did not discuss the memory, the function library and so on the complex question, only is for the practical:

First: CIN

CIN accepts one character at a time, so some people choose to define a character array and then enter it with the CIN loop, but the limitation of CIN is that it will stop accepting characters when it encounters a space;

Second: Ch1=cin.get ();

The function is also to accept a character, and then assign a value to the char type CH1, output ch1;

Third: Cin.get (CH2);

function or accept a character, and the second one, just different form, output CH2;

IV: Cin.get (buff1,6);

Here is the second use of Cin.get (), the parameter table with 3 parameters, the third parameter does not write the default is ' \ n '; Explain that the first parameter is to accept the character of the string array Buff1, the second is the number of characters to be received after +1;

V: Cin.getline (buff1,5, ' s ')

Cin.getline () is similar to Cin.get (), which is to include #include<string>; with getline and it does not output the ending character;

Sixth: Getline (CIN,STR);

The last Getline (CIN,STR); To explain the parameter table, CIN must write it up, str this is the string name that is stored after the row is fetched;

Finally, we give a piece of code that integrates six situations:

PS: My compiler is VS2012

Input and output of strings
Cin
Ch1=cin.get ();
Cin.get (CH2);

Cin.get (buff1,6);
Cin.getline (buff1,5, ' s ')
Getline (CIN,STR);
*/
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
The definitions of these two constants can be placed outside of main
#define N 10
const int s=80;

String str;
Char Ch1,ch2,ch3,ch4,ch5;
Char Buff1[s];
Char Buff2[n][s];

cout<< "Please input a string \ n" <<endl;

cin>>buff1[s];
cout<< "by cin\n";
cout<<buff1[s]<<endl<<endl;

Ch1=cin.get ();
cout<< "by Cin.get () \ n";
cout<<ch1<<endl<<endl;

Cin.get (CH2);
cout<< "by Cin.get (CH2) \ n";
cout<<ch2<<endl<<endl;

Cin.get (buff1,6);
Cin.get () The second usage, is also the input line (same as Cin.getline ()), you can add a third parameter, and getline, but the difference is, do not output delimiter ~
cout<< "by Cin.get (buff1,6) \ n";
cout<<buff1<<endl<<endl;//to note that this output is output Buff1;

Cin.getline (buff1,5, ' s ');//Pay attention to the difference from Getline
Cin.getline () has three parameters: accept string m, accept number 5, end character
cout<< "by Cin.getline (buff1,5, ' s ')" <<endl;
cout<<buff1<<endl<<endl;

Getline (CIN,STR);
Getline () is a function defined in <string>, here the parameter table means: CIN is required to have, STR is the string name that is stored after the character, wait for the output is output this str
cout<< "by Getline (CIN,STR)" <<endl;
cout<<str<<endl<<endl;

System ("pause");
return 0;
}

Character input for 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.