当前位置:网站首页>关于cin,scanf和getline,getchar,cin.getline的混合使用
关于cin,scanf和getline,getchar,cin.getline的混合使用
2022-04-23 08:12:00 【laobuzhang】
在他们混合使用时有时会出现无法输入字符串或者字符串多出空格的情况
原因如下:
cin和scanf再输入完成后会在输入缓冲区留下一个空格或者\n(看你在输入时用什么结尾的)
而下一个cin和scanf会直接忽视这个剩下的空格或者\n,读入你新的输入
但是getline,getchar,cin.getline并不会忽略这个空格或者\n,他们会把这个读入
如果cin或者scanf留下的是空格,那么下面使用getline,getchar,cin.getline输入的字符串前面会多一个空格
如果cin或者scanf留下的是\n,那么下面使用getline,getchar,cin.getline会读入该\n并且认为是你输入了\n从而直接结束输入
解决方法:
在使用完cin和scanf后,加上一句getchar()吃掉余留的空格或者\n,然后再使用getline,getchar,cin.getline
注意:
1.cout和printf并不会影响
2.getline,getchar,cin.getline本身不会残留空格或者\n
版权声明
本文为[laobuzhang]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Old_Secretary/article/details/124329201
边栏推荐
猜你喜欢

Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast

第一性原理 思维导图

RPC过程

pgsql想实现mysql一样样的列子查询操作

'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。

freertos学习02-队列 stream buffer message buffer

Qtablewidget header customization and beautification developed by pyqt5 (with source code download)

作文以记之 ~ 二叉树的前序遍历
![[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel](/img/7c/0adc0940b6d5c8a61d34bfa5f66ee7.png)
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel

【学习】从零开始的音视频开发(9)——NuPlayer
随机推荐
【学习】从零开始的音视频开发(9)——NuPlayer
How to generate assembly file
An example of network communication based on TCP / IP protocol -- file transmission
form中enctype属性
测试你的机器学习流水线
There are some problems when using numeric type to query string type fields in MySQL
Anonymous type (c Guide Basics)
Using qlst excel file
synchronized 锁的基本用法
How browser works
根据字节码获取类的绝对路径
一键清理项目下pycharm和Jupyter缓存文件
Ear acupoint diagnosis and treatment essay 0421
npm安装yarn
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
Add random attributes to the Li class array objects and sort them
基于TCP/IP协议的网络通信实例——文件传输
监控智能回放是什么,如何使用智能回放查询录像
作文以记之 ~ 二叉树的前序遍历
微信小程序 catchtap=“toDetail“ 事件问题