如果存在如下的输入,

11
is a prime

考虑如下的程序,

std::cin>>number;
std::getline(std::cin,input)

std::cin 在读取数字 11 时,没有读取 11 后面的换行符。这个换行符被之后的 std::getline 消耗了,从而导致 std::getline 读取了一个空行。

解决办法包括,

  • 使用一个额外的 getline 来消耗这个换行符;
  • 使用 std::cin >> std::ws; 来消耗这换行符;
  • 使用 cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 来忽略剩余的字符。

std::cin 和 std::getline 混用的问题的更多相关文章

  1. 关于std::cin阻塞事件循环以及控制台命令输入功能的方案;

    如果将std::cin建立在主线程,将阻塞主线程的所有事件循环,因而要保证事件循环不受影响,需要将std::cin放到 子线程中.同时,如果要实现能够强制终止std::cin阻塞, 可以通过强制终止线 ...

  2. using namespace std 和 using std::cin

    相较using std::cin使用using namespace std不会使得程序的效率变低,或者稳定性降低,只是这样作会将很多的名字引入程序,使得程序员使用的名字集合变小,容易引起命名冲突. 在 ...

  3. c++循环输入数字std::cin如何结束

    代码: #include <iostream> int main(int argc, const char * argv[]) { int sum = 0,value=0;   while ...

  4. c++中获取字符cin,getchar,get,getline的区别

    http://www.imeee.cn/News/GouWu/20090801/221298.html cin.get()与getchar()函数有什么区别? 详细点..C++中几个输入函数的用法和区 ...

  5. C++ std::unordered_map使用std::string和char *作key对比

    最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...

  6. STL的std::find和std::find_if

    std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...

  7. std::unique_lock<std::mutex> or std::lock_guard<std::mutex> C++11 区别

    http://stackoverflow.com/questions/20516773/stdunique-lockstdmutex-or-stdlock-guardstdmutex The diff ...

  8. C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  9. 解决程序出现“terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped)”的问题

    最近跑程序时出现了这么一个问题: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_al ...

  10. 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'

    error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Tra ...

随机推荐

  1. liunx服务器搭建jenkins环境

    服务器搭建jenkins 持续集成环境(1)-Jenkins安装 1)安装JDK Jenkins需要依赖JDK,所以先安装JDK1.8 yum install java-1.8.0-openjdk* ...

  2. MQ:Communications link failure

    ActiveMQ:Communications link failure问题以及解决办法   ActiveMQ版本:5.5.1 MQ 所使用的 MySQL 是 InnoDB存储引擎 记录人:@郑昀 现 ...

  3. Visual Studio Tools for Apache Cordova 使用web技术构建使用Ios Andriod 和 windows 的应用

    1.vs 开发web app 2.官方的解说

  4. jquery的y一些实用方法

    jquery的在线手册 jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code.. ...

  5. pytest用例管理框架实战(基础篇)

    先安装pip install pytest pytest用例管理框架 默认规则: 1.py文件必须以test_开头或者_test结尾 2.类名必须以test开头 3.测试用例必须以test_开头 ge ...

  6. [ACTF2020 新生赛]Include 1

    首先进入靶场可以看到trip 查看源码 点击进入提示我们能不能找到flag 可以看到这里是文件包含,想着包含index.php但是根目录是自动索引的,无论输入什么都是trip页面 又想着包含flag. ...

  7. 解读 Servlet 源码:GenericServlet,ServletConfig,ServletContext

    解读 Servlet 源码:GenericServlet,ServletConfig,ServletContext 每博一文案 人活着,就得随时准备经受磨难.他已经看过一些书,知道不论是普通人还是了不 ...

  8. SpringBoot——入门及原理

    SpringBoot 用来简化 Spring应用开发,约定大于配置,去繁从简,是由 Pivotal团队提供的全新框架.其设计目的是用来简化新 Spring应用的初始搭建以及开发过程.该框架使用了特定的 ...

  9. 机器学习算法(二): 基于鸢尾花数据集的朴素贝叶斯(Naive Bayes)预测分类

    机器学习算法(二): 基于鸢尾花数据集的朴素贝叶斯(Naive Bayes)预测分类 项目链接参考:https://www.heywhale.com/home/column/64141d6b1c8c8 ...

  10. better-scroll横向滚动、纵向滚动

    <div ref="tab" class="tab"> <ul ref="tabWrapper" class=" ...