The Cin.ignore (A,ch) method is to extract characters from the input stream (CIN), and the extracted characters are ignored (ignore) and are not used. Every character discarded, it counts and compares characters: if the count reaches a or the discarded character is CH, the Cin.ignore () function terminates; otherwise, it continues to wait. One of its most common functions is to clear the contents of input buffers that end with a carriage return, eliminating the effect of the last input on the next input. For example, it can be used this way: Cin.ignore (1024x768, ' \ n '), usually set the first parameter sufficiently large, so that in fact always only the second parameter ' \ n ' function, so this sentence is the return (including carriage return) before the character from the input buffer (stream) to clear out.
eg.
#include <iostream>
using namespace std;
void main()
{
int a, b, c;
cout << "input a:";
cin >> a;
cin.ignore(1024, ‘\n‘);
cout << "input b:";
cin >> b;
cin.ignore(1024, ‘\n‘);
cout << "input c:";
cin >> c;
cout << a << "\t" << b << "\t" << c << endl;
}
If there is no Cin.ignore (), you can enter 3 numbers at a time, separated by a space is good. But it's very ugly. That's what we want.
If Cin.ignore () does not give the parameter, then the default parameter is Cin.ignore (1,eof), which means that the 1 characters before EOF are Fu Qing off, and that no EOF is encountered to clear a character and then end, resulting in incorrect results because EOF is the end of the file identification uh.
#include<iostream>
Using namespace std;
Void main()
{
Char str1[30], str2[30], str3[30];
Cout << "Please enter your name:";
Cin >> str1;
Cout << "Please enter your address:";
Cin.ignore();
Cin.getline(str2, 30, ‘a‘);
Cout << "Please enter your hometown:";
Cin.ignore();
Cin.getline(str3, 30);
Cout << str3;
}
If you enter BCDABCD at the address then the current flow is bcd\n, at this time Cin.ignore (), eat is B, this is the flow of the left cd\n directly to Cin.getline (str3,30); n So here the getline is returned directly.
The use of the Cin.ignore () function