在一个程序里面同时用两个while(cin)的处理办法

本文转自 :http://blog.csdn.net/su_xiaoyan/article/details/8305807


//给出两个int型的vector对象,判断一个对象是否是另一个对象的前缀
#include<iostream>
#include<vector>
using namespace std;
int main()
{
 vector<int> ivec1, ivec2;
 int ival;
 //读入第一个vector对象的元素
 cout << "Enter elements for the first vector:(32767 to end)" <<endl;
 cin >> ival;
 while (ival != 32767) {
  ivec1.push_back(ival);
  cin >> ival;
 }
 //读入第二个vector对象的元素
 cout << "Enter elements for the second vector:(32767 to end)" <<endl;
 cin >> ival;
 while (ival != 32767) {
  ivec2.push_back(ival);
  cin >> ival;
 }
 //比较两个vector对象
 vector<int>::size_type size1, size2;
 size1 = ivec1.size();
 size2 = ivec2.size();
 bool result = true;
 for (vector<int>::size_type ix = 0;
  ix != (size1 > size2 ? size2 : size1); ++ix)
  if (ivec1[ix] != ivec2[ix]) {
   result = false;
   break;
  }
  //输出结果
  if(result)
   if (size1 < size2)
    cout << "The first vector is prefix of the second one."
         <<endl;
   else if(size1 == size2)
    cout << "Two vectors are equal." << endl;
   else 
    cout << "The second vector is prefix of the first one."
         << endl;
  else
   cout << "No vector is prefix of the other one."<<endl;
  return 0;
}
 
 
在程序中,对于输入文本的结束部分,使用的是32767,如果使用Ctrl+Z,有些教材给出的是:使用cin.clear();来清空输入流缓冲区。但是,只有cin.clear()是不够的。cin.clear()是用来更改cin的状态标示符的。修改以下代码:
 //读入第一个vector对象的元素
 cout << "Enter elements for the first vector:(32767 to end)" <<endl;
 cin >> ival;
 while (ival != 32767) {
  ivec1.push_back(ival);
  cin >> ival;
 }
//读入第二个vector对象的元素
 cout << "Enter elements for the second vector:(32767 to end)" <<endl;
 cin >> ival;
 while (ival != 32767) {
  ivec2.push_back(ival);
  cin >> ival;
 }
 
修改后为:
 //读入第一个vector对象的元素
 cout << "Enter elements for the first vector:(Ctrl+Z to end)" <<endl;
 while (cin >> ival) {
  ivec1.push_back(ival);
 }
 cin.clear();
 //读入第二个vector对象的元素
 cout << "Enter elements for the second vector:(Ctrl+Z to end)" <<endl;
 
 while (cin >> ival) {
  ivec2.push_back(ival);
 }
运行结果如下:
这里并没有进行第二次输入,由于在第一次输入时,有数据遗留在“输入缓冲区”中,故而cin不会等待用户输入,直接就去缓冲中读取,可是缓冲中的却不是需要的int型对象,数据再次被遗留在缓冲中,如此反复,第二个while(cin >> ival)执行完毕。
对于程序的修改,只需在
cin.clear();
下面增加一行代码:
cin.sync();
cin.sync()是用来清除缓存区的数据流的。如果标示符没有改变那么即使清除了数据流也无法输入。所以cin.clear()和cin.sync()要联合起来使用。
 
那么,对于cin.clear()和cin.sync(),参考一下程序:
#include<iostream>
using namespace std;
intmain()
{
 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;
}
 
 
除了联合使用cin.clear()和cin.sync(),还有另一个方法:
//读入第一个vector对象的元素
//……
 cin.clear();
 if(cin.get() !='\n')
 {
 //读入第二个vector对象的元素
 //……
 //比较两个vector对象
 //……
 //输出结果
 //………
 }
 else
  return -1;
  return 0;
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值