C++中标准输入流cin与Ctrl+Z使用时的问题
今天使用C++编写了一段小程序,练习使用标准库的算法,代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string> using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string; int main() {
vector<string> vec;
string str;
cout << "Please enter some string..." << endl;
while (cin >> str) {
vec.push_back(str);
}
cout << "Please enter the key word: ";
cin >> str;
auto result = find(vec.cbegin(), vec.cend(), str);
if (result == vec.cend()) {
cout << "Failed to find the key word..." << endl;
} else {
cout << "Succeed to find the key word, it is the " << result - vec.cbegin() + << "th word..." << endl;
}
system("pause");
return ;
}
然而运行时却出现了问题,运行截图如下:

从运行结果来看,第20行的cin >> str;根本没有执行,于是输出cin的状态位看看出了什么问题:
cout << "cin.fail() = " << cin.fail() << endl;
cout << "cin.bad() = " << cin.bad() << endl;
cout << "cin.eof() = " << cin.eof() << endl;

可以看出,输入流cin的failbit以及eofbit已经被置位,但badbit没有置位,这说明cin流并没有崩溃,但是流已经到达了文件结束,IO操作失败,因此cin >> str;自然没有成功执行。
而问题出现的原因在于while(cin>>str)语句在结束输入时使用了Ctrl+Z,告诉cin用户已经结束了输入,所以eofbit与failbit被置位。
为了让程序正常运行,只需调用cin.clear()让cin的所有条件状态位复位,将状态设置为有效即可。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string> using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string; int main() {
vector<string> vec;
string str;
cout << "Please enter some string..." << endl;
while (cin >> str) {
vec.push_back(str);
cout << "Push str = " << str << endl;
}
cout << "Please enter the key word: ";
cin.clear();
cin >> str;
cout << "str = " << str << endl;
cout << "cin.fail() = " << cin.fail() << endl;
cout << "cin.bad() = " << cin.bad() << endl;
cout << "cin.eof() = " << cin.eof() << endl;
auto result = find(vec.cbegin(), vec.cend(), str);
if (result == vec.cend()) {
cout << "Failed to find the key word..." << endl;
}
else {
cout << "Succeed to find the key word, it is the " << result - vec.cbegin() + << "th word..." << endl;
}
system("pause");
return ;
}
运行结果如下所示:

C++中标准输入流cin与Ctrl+Z使用时的问题的更多相关文章
- time.h文件中包含的几个函数使用时须注意事项
time.h头文件中包含以下函数 char* asctime(const struct tm *tm); char* asctime_r(const struct tm *tm,char *buf); ...
- Cocos2D中Node的userObject实例变量使用时一个要注意的地方
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在Cocos2D中,CCNode对象有一个ivar为us ...
- SQL Server中VARCHAR(MAX)和NVARCHAR(MAX)使用时要注意的问题(转载)
在Microsoft SQLServer2005及以上的版本中,对于varchar(n).nvarchar(n)和varbinary(n)有了max的扩展.可以使用如:varchar(max).nva ...
- c++中while(cin>>str)和ctrl z的相关问题探讨
对于while (cin>>str)和ctrl z的问题,网上有以下解释: -------------------------------------------------------- ...
- C++ 输入ctrl+z 不能再使用cin的问题
问题介绍: 程序步骤是开始往容器里面写数据,以Ctrl+Z来终止输入流,然后需要输入一个数据,来判断容器中是否有这个数据. 源代码如下: #include<iostream> #inclu ...
- linux中ctrl+z、ctrl+d和ctrl+c的区别
ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.ctrl+c是强制中断程序的执行,而ctrl+z的是将任务中断,但是此任务并没有结束,他仍然在进程中他只是维持挂起的状态,用户可以使用f ...
- Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中的输入流 第一节课
Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中 ...
- linux中终端控制键Ctrl+C,Ctrl+Z,Ctrl+D的使用场合
1.Ctrl+C比较暴力,就是发送Terminal到当前的程序,比如你正在运行一个查找功能,文件正在查找中,Ctrl+C就会强制结束当前的这个进程.2.Ctrl+Z是把当前的程序挂起,暂停执行这个程序 ...
- ctrl+c,ctrl+d,ctrl+z在linux中意义
ctrl+c,ctrl+d,ctrl+z在linux中意义 ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样. ctrl+c是强制中断程序的执行. ctrl+z的是将任务中断 ...
随机推荐
- C/C++中浮点数输出格式问题
在C语言中,浮点数的输出格式有三种:%g, %f, %e 首先要说的是%e是采用科学计数法来显示. %g与后两者有一个重要的差别,就是设置输出精度的时候,(C中默认浮点输出精度是6),%g认为,包括整 ...
- java反射基础知识(四)反射应用实践
反射基础 p.s: 本文需要读者对反射机制的API有一定程度的了解,如果之前没有接触过的话,建议先看一下官方文档的Quick Start. 在应用反射机制之前,首先我们先来看一下如何获取一个对象对应的 ...
- React:快速上手(3)——列表渲染
React:快速上手(3)——列表渲染 使用map循环数组 了解一些ES6 ES6, 全称 ECMAScript 6.0 ,是 JaveScript 的下一个版本标准,2015.06 发版.ES6 主 ...
- Apache 静态缓存配置
静态文件缓存 静态缓存在客户端下进行缓存,可以设置缓存文件类型与缓存时间,提升客户端访问站点速度. 语法格式 ExpiresByType type/encoding “<base> [pl ...
- ubuntu linux 1604 编译安装tesseract-ocr 4.0
主要参考官方的编译,梳理一下整个流程 Linux The build instructions for Linux also apply to other UNIX like operating sy ...
- React Native 网络请求封装:使用Promise封装fetch请求
最近公司使用React作为前端框架,使用了异步请求访问,这里做下总结: React Native中虽然也内置了XMLHttpRequest 网络请求API(也就是俗称的ajax),但XMLHttpRe ...
- [BZOJ2342]双倍回文
对每个大中心暴力找小中心即可. 代码: #include<iostream> #include<cstdio> #include<cstring> #define ...
- Limit CPUID MAX
Limit CPUID MAX to 3.是指出现在英特尔平台的BIOS设置选项.很多主板也显示成CPUID maximum value limit选项.中文意义是:限制执行CPUID指令返回数值大于 ...
- Spring注解(AOP)
底层动态代理 程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 导入aop的相关模块 <!-- https://mvnrepository.com/artifact/org. ...
- Sql Server 日期时间格式转换
日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-02-01 08:02 CONVERT(varchar(10), 时间一, 23) 结果:2 ...