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的是将任务中断 ...
随机推荐
- Servlet中参数获取方法
在web.xml里面可以定义两种参数: 一种是全局范围的参数, 一种是servlet内的参数. web.xml里定义参数的应用举例:在做分页功能时,可以在代码中直给定pageSize的值,这样,写死在 ...
- day11 函数和命名空间
# def my_sum(*args):# sum_2=0# for i in args:# sum_2+=i# return sum_2### ret=my_sum(1,2,3,4)# print( ...
- NHibernate MappingException. No Persister
在另一个Visual Studio项目(议会mm.k.Infrastructure)我有我的映射文件(一个映射目录),我的hibernate.cfg.xml和一些仓库. 这是我的映射文件: <? ...
- 数据结构&算法(二)_算法基础之前传(递归、时间复杂度、空间复杂度、二分查找)
什么是算法: 间而言之算法(Algorithm):一个计算过程,解决问题的方法 递归的两个特点: 调用自身 结束条件 递归示例: def func(x): : print("我的小鲤鱼&qu ...
- Shiro理解与总结
Feature Apache Shiro is a comprehensive application security framework with many features. The follo ...
- 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- Android bluetooth介绍
Android bluetooth介绍(一):基本概念及硬件接口Android bluetooth介绍(二): android 蓝牙代码架构及其uart 到rfcomm流程Android blueto ...
- ios 中实现storyboard 与xib 之间的切换
1,跳转到xib 假设有一个按钮,这个按钮就是实现跳转的,那么在这个按钮的点击事件中,代码可以这样写. AViewController *a1= [[AViewController alloc]ini ...
- Windows Server 2008 R2 FTP无法从外部访问的解决方法
在Windows Server 2008 R2中配置好FTP服务器后,可以在本机访问,但是无法从另一台电脑访问.原因就是在于防火墙没有配置好. 1.首先检查服务器管理器中的入站规则,确保已启用FTP服 ...
- kali 2016:mount ntfs 分区只读 --Falling back to read-only mount because the NTFS partition is in an unsafe state.
mount ntfs 分区 mount /dev/sdb1 /mnt/d 提示: The disk contains an unclean file system (0, 0).Metadata ke ...