今天使用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. beego——多种格式的数据输出

    beego当初设计的时候就考虑了API功能的设计,而我们在设计API的时候经常是输出JSON或者XML数据,那么beego提供了这样的方式直接输出: 1.JSON格式输出 func (this *Ad ...

  2. python 对象和类

    python中所有数据都是以对象形式存在.对象既包含数据(变量),也包含代码(函数),是某一类具体事物的特殊实例. 面向对象的三大特性为封装.继承和多态. 1.定义类 #定义空类 class Pers ...

  3. webdeploy启用备份 msdeploy 启用backup

    前提准备:完整安装Microsoft Web Deploy 3 下载页面:WebDeploy_amd64_zh-CN.msi msdeploy 同步站点 命令所在目录C:\Program Files\ ...

  4. LeetCode:前K个高频单词【692】

    LeetCode:前K个高频单词[692] 题目描述 给一非空的单词列表,返回前 k 个出现次数最多的单词. 返回的答案应该按单词出现频率由高到低排序.如果不同的单词有相同出现频率,按字母顺序排序. ...

  5. C#:当前时间转换成文件名

    DateTime.Now.ToFileTime().ToString(); 结果是一个字符串,类似:131238643554094913.

  6. 通过自动回复机器人学Mybatis:搭建核心架构

    imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 MessageDao.java package com.imooc.dao; impor ...

  7. 构造函数与super

    1. 当不定义构造方法,系统会为类隐式的创建一个空的无参构造方法 2. 当类定义了有参的构造方法,系统就不会为类创建无参构造方法 3. 子类中,若不显式调用super(), super()会被隐式调用 ...

  8. C\C++与Java中的static关键字

    C\C++里面的static: 面向过程的static: 在c和c++面向过程的设计里,在全局变量前加上static关键字则可将该变量定义为一个静态全局变量,比如: static int a; 那么c ...

  9. Routing in ASP.NET Web API

    Why is HttpGet required only for some actions? https://stackoverflow.com/questions/28068868/why-is-h ...

  10. Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法

    1. tx 配置方法, 代码示例 javabean及其映射文件省略,和上篇的一样 CustomerDao.java, dao层接口 public interface CustomerDao { pub ...