c++ 输入cin>> get() getline()
来源:互联网 发布:maxwell软件介绍 编辑:程序博客网 时间:2024/05/21 03:19
最近终于把三种输入方式搞明白了,记下来。
cin
> char n1[20];> char n2[20];> cout<<"Enter your name: "<<endl;> cin>>n1;> cout<<"Enter your school: "<<endl;> cin>>n2;> cout<< "your name is "<<n1<<endl;> cout<<"your school is"<<n1<<endl;
运行情况1:
> Enter your name: > **Taylor Swift**> Enter your school: > your name is Taylor;> your school is swift
分析:cin 使用空白(空格、制表符、换行符)来确定字符串的结束位置,所以我们在还没有对“输入学校”做出反应,程序就把他显示出来了,taylor 后面的空格 结束了第一次输入后,把taylor 放到n1中,并自动在结尾添加空字符(‘\0’)
cin 把第一个单词读取完后,把 空格swift回车 留在输入队列中,而下面cin>>n2 的输入会过滤掉不可见的字符(指空格,制表符,回车),首先把 空格 过滤,然后把 swift 放到n2中,遇见回车 结束。(关于cin的读入规则,要把前面这加粗的三句话理清楚,就没问题了!)
geline() 和 get()
istream中的类(如cin)提供了面向行的类成员函数:getline() get() 他们都以换行符作为结束,但是getline()会丢弃换行符,get()会把换行符保留在输入队列中
1、getline() 读取字符数组
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
char name[20];cin.getline(name,20);
2、getline() 读取string对象
(1) istream& getline (istream& is, string& str, char delim);
(2) istream& getline (istream& is, string& str);
string str;getline(cin,str);
3、 get()
char name[20];char sch[20];cin.get(name,20);cin.get();//读取下一个字符串(即使是换行符),用它来处理换行符cin.get(sch,20);
阅读全文
0 0
- c++ 输入cin>> get() getline()
- C++: cin.get()与cin.getline()
- 各种输入方式的细节cin.get/cin.getline/getline...
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ 输入函数 cin>>、cin.getline()和cin.get()区别
- C++ cin.getline() , cin.get(), getline()
- cin、cin.get()、getline()
- cin、cin.get()、getline()
- cin.get(),cin.getline()
- cin cin.get cin.getline
- cin cin.get cin.getline
- cin,cin.get,cin.getline.
- cin cin.get cin.getline
- cin,cin.get(),cin.getline()
- cin gets getline cin.get等输入的区别
- Oracle-SQL
- BeanUtils.copyProperties()方法和PropertyUtils.copyProperties()的区别 [转]
- HDU
- Struts2的自动封装注入
- 广度优先搜索(入门)
- c++ 输入cin>> get() getline()
- Math对象常用属性与方法详解
- ubuntu系统cmake升级
- HDOJ2108 判断凸多边形
- 汪国新、尼玛泽仁、郑桂兰等58位艺术家参与诗书画意颂中国深圳行采风
- dede arclist里field:body的调用方法
- Linux JDK安装和坏境变量配置
- hdoj 1325 Is It A Tree?
- [日推荐] 『Streeter』极乐商店邀你一起来尬舞啊!-store.dreawer.com