When learning C++, these input functions are a little confusing; Here’s a summary:
Cin
2 cin. Get ()
3 cin. Getline ()
4 getline()
5 gets()
6 getchar()
Add: cin. Ignore (); Cin. Get ()// skips a character, such as unwanted carriage return, space, etc
1, cin> >
Usage 1: The most basic and commonly used usage is to enter a number:
#include < iostream>
using namespace std;
main ()
{
int a,b;
cin> > a> > b;
cout< < a+b< < endl;
}
Input: 2[enter]3[enter]
output: 5
Note: & gt; > Is to filter out invisible characters (such as space return, TAB, etc.)
cin> > noskipws> > input[j]; // If you don’t want to skip white space characters, use noskipWS flow control
Usage 2: Accept a string and end with “space”, “TAB”, or “enter”
#include < iostream>
using namespace std;
main ()
{
char a[20];
cin> > a;
cout< < a< < endl;
}
Input: JKLJKLJKL
output: JKLJKLJKL
Input: JKLJKL // end of blank
output: JKLJKL
2, cin. The get ()
Usage 1: Cin. Get (character variable name) can be used to receive characters
#include < iostream>
using namespace std;
main ()
{
char;
ch = cin. The get ();// or cin. Get (ch);
cout< < ch< < endl;
}
Input: JLJKLJKL
output: j
Usage 2: Cin. Get (character array name, number of characters to receive) is used to receive a line of strings and can receive Spaces
#include < iostream>
using namespace std;
main ()
{
char a[20];
cin. Get (a, 20);
cout< < a< < endl;
}
JKL JKL
output: JKL JKL JKL
Input: abcdeabcdeabcdeabcdeabcde 25 characters (input)
output: abcdeabcdeabcdeabcd receive (19 + 1 character ‘\ 0’)
Usage 3: Cin. Get (without parameters) No parameters are mainly used to discard the unnecessary characters in the input stream, or to discard carriage return to make up for the deficiency of Cin. Get (character array name, number of characters received) (because cin. Get () retains the newline character in the input buffer, cin.
3. Cin. Getline () // accepts a string and can receive Spaces 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, the last of which is ‘\0’, so you only see 4 characters output;
If 5 is changed to 20:
input: JKLJKLJKL
output: JKLJKLJKL
JKLF FJLSJF FJSDKLF
output: JKLF FJLSJF FJSDKLF
Cin getline()
//cin. Getline () actually has three parameters, cin. Getline (accepts string storage space m, accepts 5, ending characters)
// when the third parameter is omitted, the system default is ‘\n’
// if cin. Getline () in the example is changed to cin. When JLKJKLJKL is input, JKLJ is output, while when jkaljkljkl is input, jk is output. At this point, the status flag bit of cin is false (as long as the number of inputs exceeds the number of accepts, cin. Clear () is needed if getline() is used afterwards,
Cin getLine (M [I],20) can also be used when using a multi-dimensional array.
#include< iostream>
#include< string>
using namespace std;
{
char m[3][20];
the for (int I = 0; i< 3; I++)
{
cout< <” \n Please enter “< < i+1< <” The string: “< < endl;
cin. Getline (m [I], 20);
}
Cout< < endl;
the for (int j = 0; j< 3; J++)
cout< <” Output m [” & lt; & lt; j< & lt; “] Value: “& lt; < m[j]< < endl;
}
Please enter the first string:
kskr1
Please enter the second string:
kskr2
Please enter the third string:
kskr3
Output m[0] value :kskr1
output m[1] value :kskr2
output m[2] value :kskr3
Getline () // getLine () // getLine () // getLine () // getLine () // getLine () // getLine () // getLine () // getLine () // getLine () 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
Cin is similar to Cin. Getline (), but cin. Getline () belongs to istream stream while getLine () belongs to String stream and is a different function
Read More:
- In C + + cin.getline The difference between () and getline () functions
- C++ cin.ignore Use of ()
- use cin.get () instead of system (“pause”) to avoid the flash of C + + programs
- C / C + + rounding function: round function
- C / C + + rounding function ceil(), floor ()
- The function and usage of argc and argv in C language
- C / C + + library function (tower / tower) realizes the conversion of letter case
- The use of C + + template function and lambda expression
- Python – get the information of calling function from called function
- How to use C + + function pointer array
- The usage and difference of atoi() and stoi() functions in C + +
- Error c2064: term does not evaluate to a function in VC
- [C + +] C + + overload operator = must be a nonstatic member function?
- C + + pauses the black window system (“pause”); (get ch(), getchar(), system (pause)’s connection and difference
- PHP function file_ get_ Contents() reports an error when using HTTPS protocol: SSL operation failed
- Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“
- Error c2137 of C language: empty character constant (Fixed)
- C++:error C2228: left of ‘.str’ must have class/struct/union
- error C4996: ‘scanf‘: This function or variable may be unsafe.Visual Studio Series compilers report errors using scanf function
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)