When you learn C + +, you must be confused about the functions of the input and output functions, now to summarize the simple use of various functions.
CIN has a buffer, the input buffer, that is built. The first input process is this, when the input data is stored in the input buffer at the end of the keyboard input, and the CIN function takes the data directly from the input buffer. Because the CIN function takes data directly from the buffer, sometimes when there is residual data in the buffer, the CIN function directly obtains the residual data without requesting the keyboard input, which is why sometimes the input statement fails.
Usage of the 1.cin>> function: This is the input stream we use most often to learn the initial contact of C + +.
Usage 1. Accepts the input of a string and encounters a "Space" "tab" "Enter" end.
#include <iostream>
using namespace Std;
void Main ()
{
Char a[10];
cin>>a;
cout<<a<<endl;
}
Input: AAABBBCCC
Output: AAABBBCCC
Input: AAA BBB CCC
Output: AAA
2. Enter a number.
#include <iostream>
using namespace Std;
void Main ()
{
int A, B;
cin>>a>>b;
cout<<a+b<<endl;
}
Input: 1 return car 2 return car
Output: 3
The use of 2.cin.get (), this function has three forms, Cin.get (), Cin.get (char ch), Cin.get (array,length).
Usage 1.cin.get ()
Used to read a character, the end condition is carriage return, but does not discard the buffer's carriage return (enter) and the space, Cin.get () and Cin.get (char ch) function basically the same, char = Cin.get () and Cin.get (char ch) are the same.
#include <iostream>
using namespace Std;
int main ()
{
Char c,ch;
c = Cin.get ();
Cin.get (CH);
cout<<c<<endl;
cout<<ch<<endl;
return 0;
}
Input: A return (enter a character and then enter)
Output: A
(One line here)
Input: ABCD return
Output: A
B
Usage 2.cin.get (char ch) Usage 1
Usage 3.cin.get (array,length) accepts the input of a string, accepts the input length-1 characters, and the last deposit is ' \ s '.
#include <iostream>
using namespace Std;
int main ()
{
Char ch[10];
Cin.get (ch,5);
cout<<ch<<endl;
return 0;
}
Input: ABCDEFG return
Output: ABCD
The use of 3.cin.getline (), which accepts a string of input including a space, encounters a carriage return stop.
Usage 1.cin.getline ()
#include <iostream>
using namespace Std;
int main ()
{
Char ch[10];
Cin.getline (ch,5);
cout<<ch<<endl;
return 0;
}
Input: AAAAAAAA
Output: AAAA
The 4.getline () function, which accepts a string input containing a space, encounters a carriage return stop to include #incldue<string>.
Usage 1.getline (cin,sting s)
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
string S;
Getline (cin,s);
cout<<s<<endl;
return 0;
}
Input: ABCDEFG return
Output: ABCDEFG
The 5.gets () function, which accepts a string input containing a space, encounters a carriage return stop, to contain #incldue<string>.
Usage 1: Receives input from a string.
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
Char ch[10];
Gets (CH);
cout<<ch<<endl;
return 0;
}
Input: ABCDEFG return
Output: ABCDEFG
6.getchar () accepts a character input, contains spaces, and encounters a carriage return stop, to include #incldue<string>.
Usage 1: Accept input of one character
#include <iostream>
#include <string>
using namespace Std;
int main ()
{
Char ch;
ch = getchar ();
cout<<ch<<endl;
return 0;
}
Input: ABC return
Output: A
Summary of use of CIN in C + +