C + + for most people treats Cin::sync () as an error to clear the buffer function

Source: Internet
Author: User

Baidu Cin::sync (), the majority of the interpretation is used to clear the buffer, and declare the general and Cin::clear () function together to achieve the purpose.

The same way Baidu clears the buffer, and most of it says to use Cin::sync () to achieve this goal.

   

   However, Cin::sync () is not used to clear the buffer, so sometimes it does not do what we want to empty the buffer stream.

http://www.cplusplus.com/reference/istream/istream/sync/

For Cin::sync, the effect is based on the above mentioned C + + documentation, which should be: Synchronize input buffer, which is the synchronous inputs buffered stream .

Take a look at the examples in this webpage:

   

Syncing input stream#include <iostream>     //Std::cin, Std::coutint Main () {  char First, second;  Std::cout << "Please, enter a word:";  First = Std::cin.get ();  Std::cin.sync ();  Std::cout << "Please, enter another word:";  Second = Std::cin.get ();  Std::cout << "The first word began by" << first << ' \ n ';  Std::cout << "The second word began by" << second << ' \ n ';  return 0;}

This example demonstrates how sync behaves on certain implementations cin of, removing any unread character from the Standard input queue of characters.

Possible output:

  

Please, enter a word:test. Another word:text the first word began by T of the second word began by T

(because the page layout is always problematic, the following is written in code format:

However, when we implement this example on VS2017, we do not get the result, and we get the result:

(break point) Why is this so? Notice the results of this example on this page:Possible output OK, now to explain what Cin::sync () is doing. There are times when it can be used as an empty input buffer stream, but this is sometimes done. First, we enter the text from the keyboard, press ENTER, text enters the input buffer stream (including carriage return), and then takes a character ' t ' to assign to it, and removes ' t ' from the buffer stream, then enters the key Cin::sync (); At this point the content of the data source is: Text (VS2017), the contents of the buffer is: Ext through Cin::sync (); Synchronizes the contents of the data source with the buffer, the contents of the buffer becomes the text at this time the position of the stream is E, so when the content from the buffer is taken, E instead of T
Why is it possible to erase the contents of a buffer with Cin::sync () under a compiler such as devc++? Because in the devc++, the data source after the data to the buffer, the content of the data source is emptied, so in the synchronization of Cin::sync (), the buffer is also emptied, so that the buffer is emptied, the buffer is empty when the data is re-fetch, and then again from the keyboard (data source) to type , and then to the buffer step, under this step, the position of the flow is also "refreshed", so there is no output E, but output t

  


If anything is wrong, please correct me.

C + + for most people treats Cin::sync () as an error to clear the buffer 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.