"C + +" input stream cin method __c++

Source: Internet
Author: User
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)

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.