Input stream control: several common functions to clear spaces or carriage returns in the input stream

C++ input stream control: several common functions to clear spaces or carriage returns in the input stream

Usage of cin.clear(), cin.sync() and cin.ignor()

一、cin.clear()、cin.sync()

Cin.clear() is used to change the status indicator of cin.
Cin.sync() is used to clear the data stream in the buffer.
If the identifier has not changed, it cannot be entered even if the data stream is cleared. So the two should be used in combination. E.g:

#include<iostream>
using namespace std;
int main()
{
    
    
	int a;
	cout<<"输入一个字母:"<<endl;
	cin>>a;	//int 型变量中放了char 型数据,failbit 置 1
	cout<<"cin.fail()="<<cin.fail()<<endl;	//输出 1
	//cin.clear();
	//cin.sync();
	cout<<"输入一个数字:"<<endl;	//由于failbit 值为 1,输入流不能正常工作
	cin>>a;//故此处的输入无效
	cout<<a<<endl;	//输出不确定值
	cin.clear();	//此处用cin.clear()流标志复位
	//cin.sync();
	cout<<"cin.fail()="<<cin.fail()<<endl;	//此处failbit 已为 0
	cout<<"输入一个数字:"<<endl;
	//但刚才输入的字符并没有从流中清除,所以cin>>a 又把那个字符放入a 中,流输入流又不能正常工作
	cin>>a;
	cout<<a<<endl; //输出不确定值
	cout<<"cin.fail()="<<cin.fail()<<endl;	//在此处failbit 又为 1 
	cin.clear();	//再次修复输入流
	cin.ignore();	//取走刚才流中的字符
	cout<<"输入一个数字:"<<endl;	//再次接收用记输入,这次输入数字,正常输出了
	cin>>a;
	cout<<"a="<<a<<endl;
	// 现在再看一下输入流的failbit
	cout<<"cin.fail()="<<cin.fail()<<endl;//输出 0,表明输入流已恢复正常
	return 0;
}

2. Cin.ignore(a,ch)
extracts characters from the input stream (cin), and the extracted characters are ignored (ignore) and not used. Every time a character is discarded, it counts and compares characters: if the count value reaches a or the discarded character is ch, the cin.ignore() function is terminated; otherwise, it continues to wait.
One of its common functions is to clear the contents of the input buffer that ends with a carriage return and eliminate the influence of the previous input on the next input. For example, it can be used like this:

cin.ignore(1024,'\n')

Usually the first parameter is set to be large enough, so that in fact only the second parameter'\n' works, so this sentence is to put all the characters before the carriage return (including carriage return) from the input buffer (stream) Cleared out.

3. Comparison of cin.sync() and cin.ignor()

The role of sync() is to clear the input buffer. It returns 0 on success, and the badbit is set on failure, and the function returns -1. In addition, for the input stream bound to the output, calling sync() will refresh the output buffer.
However, since the progress of the external input is not always known when the program is running, it is difficult to control whether all the contents of the input buffer are cleared. Usually we may just want to give up part of the input buffer, but not all. For example, clear the current line, or clear the newline at the end of the line, and so on. But if there is already the content of the next line in the buffer, this part may be something we want to keep. It is best not to use sync() at this time. Consider using ignore function instead.

	cin.ignore(numeric_limits<std::streamsize>::max(),'/n');//清除当前行
	cin.ignore(numeric_limits<std::streamsize>::max());	//清除cin 里所有内容

Don't be intimidated by the long name. Numeric_limits<std::streamsize>::max() is just the maximum value used by the stream defined by the climits header file. You can also replace it with a large enough integer. Using ignore can obviously control the buffer more accurately than sync().

Guess you like

Origin blog.csdn.net/hyl1181/article/details/107488425