cin cout getline string
1.C++ code, When we want to read a number whatever the type is int or double , just use
cin >> Num_variable;
Then, after read number, if we want to read a line of words, we have to use
cin.ignore();
and next line
getline(cin, String);
after we read a Num_variable, a '\n' char is still saved in the input buffer, the "cin.ignore()" can clear the input buffer
so, the String can be set to really what you want to read, otherwise if we don't use cin.ignore(), code will read blank line("blank\n") to String.
e.g.,
cin >> NUM;
cin.ignore();
getline(cin, String0);
getline(cin, String1);
getline(cin, String2);
·······
2. C++ code, When we want to print a double type of number, we have to set the precision of this number.
we have to #include <iomanip>
before cout, we can use
cout << setiosflags(ios::fixed);
and next line
cout << setprecision(1) << e << endl;
code will print 1 number after the number_point.
cin cout getline string的更多相关文章
- C++语言中cin cin.getline cin.get getline gets getchar 的用法实例
#include <iostream> #include <string> using namespace std; //关于cin cin.getline cin.get g ...
- cin 和 getline 混用中需要注意的问题
这段时间在刷题过程中遇到一个cin和getline混合使用中的问题,解决之后记录如下: 先来看一段代码 #include <iostream> #include <string> ...
- 洛谷P1765 手机_NOI导刊2010普及(10) 关于cin和getline的一些区别 以及一些STL
一. cin>>s:cin>>是由两部分构成的,cin和>>,其中cin是输入流istream类的一个对象,隶属于iostream函数库而>>则是运算符 ...
- acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
题目 输入正好是long long的最大, 但是答案超long long 所以用unsigned, 不能用cin cout否则一定超时: 不能用abs(), abs 只用于整数. unsigned ...
- printf scanf cin cout的区别与特征
printf和scanf是c语言的输入输出,学习c++以后,自然是用cin cout这两个更简单的输入输出 printf scanf 都需要进行格式控制,比较麻烦,但优点是速度比较快,毕竟多做了一些事 ...
- C++输入输出流 cin/cout 及格式化输出简介
C++ 可通过流的概念进行程序与外界环境( 用户.文件等 )之间的交互.流是一种将数据自源( source )推送至目的地( destination )的管道.在 C++ 中,与标准输入/输出相关的流 ...
- 892B. Wrath#愤怒的连环杀人事件(cin/cout的加速)
题目出处:http://codeforces.com/problemset/problem/892/B 题目大意:一队人同时举刀捅死前面一些人后还活着几个 #include<iostream&g ...
- 在c++中,标准输入string时cin 与getline两个函数之间的区别
cin: cin函数是标准库的输入函数,在读取string时遵循以下规则: 1)读取并忽略开头所有的空白符(包括空格.换行符.制表符). 2)读取字符直到遇到空白符,读取终止. 例如: 当输入的是“ ...
- scanf printf gets() puts(),cin cout
最近在练机试题,常用的C和C++输入输出如下: 1 scanf 和printf int a; scanf("%d",&a) ; printf("%d", ...
随机推荐
- Ubuntu源更新
Ubuntu12.04的源在 /etc/apt/sources.list 中, 进入 /etc/apt/ 先进行备份 然后用根用户权限打开sources.list. sudo gedit /etc/ ...
- eclipse 不能启动虚拟机
在eclipse文件下有个eclipse.ini文件打开它.现在如果没有-vm 这个参数那么就在最后新起一行写上 -vm+空格+参数 参数是你安装的jdk目录比如我的 -vm d:/java/jdk1 ...
- ROS中使用ABB Yumi IRB14000的一些资料汇总
目前,ABB RobotStudio 已经更新到6.05.01了,可至官网下载. 使用ABB RobotStudio和ROS进行联合调试,请参考下文: http://blog.csdn.net/Zha ...
- Kotlin For Gank.io (干货集中营Kotlin实现)
介绍 Kotlin,现在如火如荼,所以花了一点时间把之前的项目用Kotlin重构一下 原项目地址:https://github.com/onlyloveyd/GankIOClient 对应Kotlin ...
- 使用sqlmap执行SQL注入攻击
sqlmap是开源的SQL注入自动攻击工具,它可以自动的探测SQL注入点并且进一步控制网站的数据库. Kali Linux默认安装了这个工具. 找到潜在的攻击目标 第一步是找到有SQL注入漏洞的网站. ...
- cocos2d-x3.16 default模式项目 Android Studio C++文件编译失败问题
gradle sync正常,但是在编译的时候几乎自己写的Classes里全部c++文件的最后一行都在报错,原来是3.16 模板cpp-template-default内的Android.mk文件内这一 ...
- 【前端】JavaScript入门学习
<button type="button" onclick="alert('hillo!')">Here</button> <sc ...
- HDU - 6178:Monkeys (贪心&树上最大匹配输&输入优化)
There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occu ...
- 剑指offer-第五章优化时间和空间效率(两个链表的第一个公共节点)
思路1:要求的是两个链表的第一个公共节点,首先想到的是用栈来存放两个链表,然后依次从栈中抛出,直到最后一个相同的节点为止.但是要用到两个栈,空间复杂度为O(n): 思路2:从头到尾分别遍历两个链表得到 ...
- 在Spring中通过构造自动装配--constructor
在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数类型自动装配. 这意味着,如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么将自动装配. packag ...