The use of the Cin.ignore () function

Source: Internet
Author: User

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

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.