Check InputCIN will check the input format and return FALSE if the input does not match the expected format.
cout << "Enter numbers:";
int sum = 0;
int input;
while (CIN >> input)
sum + = input;
cout << "Last value entered =" << input << Endl;
cout << "sum =" << sum << Endl;
The above check can be placed in a try, catch statement.
try{while
(cin >> input)
sum + = input;
catch (Ios_base::failure &f) {
cout<<f.what () <<endl;
}
string Input: Getline (), get (), ignore ()Getline () reads the entire row, reads a specified number of characters, or stops reading when the specified character (the default line break) is encountered. Get () is the same as getline () and accepts the same arguments, and stops reading when reading to a specified number of characters or line breaks. But get () stops the line break in the input stream, that is, the first character read by the next function will be a line feed. Ignore () accepts parameters that are also getline (), ignoring the specified number of characters or to line breaks.
Char Input[limit];
cout << "Enter a string for getline () processing:\n";
Cin.getline (input, Limit, ' # ');
cout << "Here is your input:\n";
cout << input << "\ndone with phase 1\n";
char ch;
Cin.get (CH);
cout << "The next input character is" << ch << endl;
if (ch!= ' \ n ')
Cin.ignore (Limit, ' \ n '); Ignore the remainder of this line
cout << "Enter a string for Get () processing:\n";
Cin.get (input, Limit, ' # ');
cout << "Here is your input:\n";
cout << input << "\ndone with phase 2\n";
Cin.get (CH);
cout << "The next input character is" << ch << endl;
Using Getline () in the example, the next get () reads the character 3, ignores the #, and get () after the #在流中, so read the character #.
Read ()Reads the specified array character, similar to the Write () method.
Char score[20];
Cin.read (score,20);
putback ()Putback () inserts a character into the input character, and the inserted character is the first character read by the next statement.
char ch;
while (Cin.get (CH)) //terminates on EOF
{
if (ch!= ' # ')
cout << ch;
else
{
cin.putback (CH); Reinsert character break
;
}
Cin.get (CH);
cout << endl << ch << "is next input character.\n";
Peek ()Returns the next character in the input stream, but only the view is not extracted.
Char input[100];
char ch;
int i=0;
while ((Ch=cin.peek ())!= '. ' &&ch!= ' \ n ')
cin.get (input[i++]);
Input[i]= ' ";
The program encounters a period or a line feed loop stop. A period or newline character remains in the input stream. As you can see, using peek is the equivalent of reading a character with Get () and putting the character into the input stream with putback ().
See "Input stream cin method"
* Reference: "C + + Primer Plus 5nd"
(reprint please indicate author and origin: Http://blog.csdn.net/xiaowei_cqu do not use for commercial use without permission)