cin.getline,cin.get,cin.ignore,cin.clear,cin.sync

1、cin.getline()
cin.getline()方法连续地从用户终端接受字符,并将字符存入字符型数组message中,直到输入了(maxchars-1)个字符(第maxchars个字符用来存储字符串结尾的NULL字符'')或者接受到了回车为止,这终端键入回车键产生一个换行'n',它被cin.getline()认为是行输入结尾。cin.getline()获得的字符(除了换行符外)被存储到message数组中。在返回之前,cin.getline()函数在存储的这些字符后面添加一个NULL字符''。
2、cin.get()
cin.get(name, ArSize);
cin.get(dessert, Arsize); // a problem
Because the first call leaves the newline in the input queue, that newline is the first character the second call sees. Thus, get() concludes that it's reached the end of line without having found anything to read. Without help, get() just can't get past that newline.
Fortunately, there is help in the form of a variation of get(). The call cin.get() (no arguments) reads the single next character, even if it is a newline, so you can use it to dispose of the newline and prepare for the next line of input. That is, this sequence works:
cin.get(name, ArSize); // read first line
cin.get(); // read newline
cin.get(dessert, Arsize); // read second line
Another way to use get() is to concatenate, or join, the two class member functions as follows:
cin.get(name, ArSize).get(); // concatenate member functions
What makes this possible is that cin.get(name, ArSize) returns the cin object, which then is used as the object that invokes the get() function. Similarly, the statement
cin.getline(name1, ArSize).getline(name2, ArSize);
reads two consecutive input lines into the arrays name1 and name2; it's equivalent to making two separate calls to cin.getline().

3、Cin.ignore()
Cin.ignore()方法cin.ignore(5, 'c')的是从输入流(cin)中提取字符,提取的字符被忽略(ignore),不被使用。每抛弃一个字符,它都要计数和比较字符:如果计数值达到5或者被抛弃的字符是'c',则cin.ignore()函数执行终止;否则,它继续等待。它的一个常用功能就是用来清除以回车结束的输入缓冲区的内容,消除上一次输入对下一次输入的影响。比如可以这么用:cin.ignore(1024, 'n');通常把第一个参数设置得足够大,这样实际上总是只有第二个参数'n'起作用,所以这一句就是把回车(包括回车)之前的所以字符从输入缓冲(流)中清除出去。
4、cin.sync()
清空输入缓冲区的内容
在输入规定的数目float型数据中错误输入一个string型,然而后提示输入错误,转重新输入,直到输入正确,可以执行下面的程序
int x;
cin >> x;
while (cin.fail())
{
cin.clear();
cin.sync();
cout << "XXXXX"<> x;
}

5、Cin.clear()
Cin.clear()用法如果输入发生错误发生,那么流状态既被标记为错误,你必须清除这些错误状态,以使你的程序能正确适当地继续运行。要清除错误状态,需使用clear()函数。此函数带一个参数,它是你将要设为当前状态的标志值,只要将ios::goodbit作为实参。

#include
using namespace std;
void main()
{
int a;
cout<<"输入一个字母:"<>a; //int型变量中放了char型数据,failbit置1
cout<<"cin.fail()="<>a; //故此处的输入无效
cout<>a又把那个字符放入a中,流输入流又不能正常工作
cin>>a;
cout<>a;
cout<<"a="<
  另一种方法则是使用下面任何一个函数来检测相应的输入/输出状态:
bool bad();
bool eof();
bool fail();
bool good();

  下例示例,表示出了上面各成员函数的用法:
#include
using namespace std;
int main()
{
int a;
cin>>a;
cout<
  如果错误发生,那么流状态既被标记为错误,你必须清除这些错误状态,以使你的程序能正确适当地继续运行。要清除错误状态,需使用clear()函数。此函数带一个参数,它是你将要设为当前状态的标志值。,只要将ios::goodbit作为实参。
  示例代码如下:
#include
using namespace std;
int main()
{
int a;
cin>>a;
cout<
通常当我们发现输入有错又需要改正的时候,使用clear()更改标记为正确后,同时也需要使用get()成员函数清除输入缓冲区,以达到重复输入的目的。
  示例代码如下:
#include
using namespace std;

int main()
{
int a;
while(1) //也可以写成for(;1;)
{
cin>>a;
if(!cin)//条件可改写为cin.fail()
{
cout<<"输入有错!请重新输入"<
  最后再给出一个对文件流错误标记处理的例子,巩固学习,代码如下:

#include
#include
using namespace std;

int main()
{
ifstream myfile("c:\1.txt",ios_base::in,0);
if(myfile.fail())
{
cout<<"文件读取失败或指定文件不存在!"<

点赞 (0)

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code