今天使用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使用时的问题的更多相关文章

  1. time.h文件中包含的几个函数使用时须注意事项

    time.h头文件中包含以下函数 char* asctime(const struct tm *tm); char* asctime_r(const struct tm *tm,char *buf); ...

  2. Cocos2D中Node的userObject实例变量使用时一个要注意的地方

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在Cocos2D中,CCNode对象有一个ivar为us ...

  3. SQL Server中VARCHAR(MAX)和NVARCHAR(MAX)使用时要注意的问题(转载)

    在Microsoft SQLServer2005及以上的版本中,对于varchar(n).nvarchar(n)和varbinary(n)有了max的扩展.可以使用如:varchar(max).nva ...

  4. c++中while(cin>>str)和ctrl z的相关问题探讨

    对于while (cin>>str)和ctrl z的问题,网上有以下解释: -------------------------------------------------------- ...

  5. C++ 输入ctrl+z 不能再使用cin的问题

    问题介绍: 程序步骤是开始往容器里面写数据,以Ctrl+Z来终止输入流,然后需要输入一个数据,来判断容器中是否有这个数据. 源代码如下: #include<iostream> #inclu ...

  6. linux中ctrl+z、ctrl+d和ctrl+c的区别

    ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.ctrl+c是强制中断程序的执行,而ctrl+z的是将任务中断,但是此任务并没有结束,他仍然在进程中他只是维持挂起的状态,用户可以使用f ...

  7. Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig CentOS远程连接 Linux中的输入流 第一节课

    Centos安装自定义布局才能自己划分各个区的大小ctrl+z ,fg ,route -n ,cat !$ ,!cat ,XShell 设置, ifconfig  CentOS远程连接  Linux中 ...

  8. linux中终端控制键Ctrl+C,Ctrl+Z,Ctrl+D的使用场合

    1.Ctrl+C比较暴力,就是发送Terminal到当前的程序,比如你正在运行一个查找功能,文件正在查找中,Ctrl+C就会强制结束当前的这个进程.2.Ctrl+Z是把当前的程序挂起,暂停执行这个程序 ...

  9. ctrl+c,ctrl+d,ctrl+z在linux中意义

    ctrl+c,ctrl+d,ctrl+z在linux中意义   ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.   ctrl+c是强制中断程序的执行.   ctrl+z的是将任务中断 ...

随机推荐

  1. 词性标注算法之CLAWS算法和VOLSUNGA算法

    背景知识 词性标注:将句子中兼类词的词性根据上下文唯一地确定下来. 一.基于规则的词性标注方法 1.原理 利用事先制定好的规则对具有多个词性的词进行消歧,最后保留一个正确的词性. 2.步骤 ①对词性歧 ...

  2. LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: --异常记录

    升级了JDK之后,启动应用,直接抛出此异常.网上搜罗半天,没有正确的解决方案. 然后想到了是“升级了JDK”,重新检查所有JDK相关的配置的地方,在Debug Configurations里找到启动时 ...

  3. CNN学习笔记:目标函数

    CNN学习笔记:目标函数 分类任务中的目标函数 目标函数,亦称损失函数或代价函数,是整个网络模型的指挥棒,通过样本的预测结果与真实标记产生的误差来反向传播指导网络参数学习和表示学习. 假设某分类任务共 ...

  4. 450. Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  5. 格式化字符串--format用法

    print("hello {leon}".format(leon="world!")) #format 是一个格式化字符穿的方法. print("he ...

  6. redis 笔记05 Sentinel、集群

    Sentinel 1. Sentinel只是一个运行在特殊模式下的Redis服务器,它使用了和普通模式不同的命令表,所以Sentinel模式能够使用的命令和普通的Redis服务器能够使用的命令不同. ...

  7. mysql中int(M) tinyint(M)中M的作用

    原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int(11)是指11个字节,int(10)就是10个字节.我错了. http://zhidao.baidu.com/li ...

  8. CentOS6升级Apache-httpd2.4.29

    本文档解决AppacheHttp由版本2.2.x升级到版本2.4.29的问题,安装需要先进行依赖软件包的安装,请检查相应依赖软件包安装情况,如系统已经正确安装相应依赖软件包,可略过,本文所述版本升级不 ...

  9. 用来在category里加属性的宏

    众所周知,一般的情况下我们是没办法在category里加属性的. 如果想加,需要用到Associated. @interface NSObject (XYFlyweightTransmit) @pro ...

  10. 深入理解JVM 垃圾收集器(下)G1收集器

    1.回顾CMS 1.1堆内存结构 1.2新生代GC 1.3老年代GC 2.G1收集器 2.1G1实现概览及使用场景 G1的推荐使用场景 2.2GC 2.2.1新生代GC 2.2.2老年代GC 老年代G ...