C + + Cin.sync () and Cin.ignore () __c++

Source: Internet
Author: User

The function of Cin.sync () is to empty the buffer, while Cin.ignore () is also the role of the data in the delete buffer, but it controls the deletion data in the buffer more precisely.

Sometimes you just want to take a part of the buffer and discard the other part, which is the Cin.ignore () that can be used:

Cin.ignore (int intexp, char chexp);

Where intexp is an integer expression or an integer value that represents the maximum number of characters ignored in a line, such as intexp=100, and a parameter chexp, a character expression. Indicates that if you encounter a character value equal to Chexp, then stop ignore (), and if you have not encountered a character with a value equal to Chexp after ignore100 a character, you must stop ignore (), so 100 is the maximum number of characters ignored by ignore ().
Here are a few examples

#include <iostream>

#include <cstdlib>

int main ()

{

int ival1 = 0, ival2 = 0;

Std::cin >> Ival1;

Std::cin.ignore (+, ' \ n ');

Std::cin >> Ival2;

Std::cout << "Ival1 =" << ival1 << Std::endl;

Std::cout << "Ival2 =" << ival2 << Std::endl;

System ("pause");

return 0;

}


① press ENTER, IVAL1 receives 12, and the rest is cleared, because enter is itself a blank line character, and the input stream waits for the second input to be ival2 assignment. If there is no middle sentence std::cin.ignore (+, ' \ n '), then will not wait for the second input, direct output Ival1 = Ival2 = 34:


② std::cin.ignore ("\ n") to Std::cin.ignore (2, ' \ n '), ival1 after receiving 12, ignore clear out two characters:


Why is Ival2 4 instead of 78?

because the IO object we use is to manipulate char data , regardless of what data we enter, the CIN cout is converted to char to handle, for example, we want to output the value of an cout variable, so before the output, cout converts the value of the variable to a character, in the output (c + + Primer Plus, there is one sentence: In essence, the c++insertion operatoradjusts it behavior to fit the type of DA Ta that follows it.), so the top ignore clears a space and a character 3, so the buffer is left with 4, 56, 78, so ival2 equals 4.

③ if Cin.ignore () does not give a parameter, the default argument is Cin.ignore (1,eof), which is to Fu Qing the 1 words before EOF.


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.