C++语言中cin cin.getline cin.get getline gets getchar 的用法实例
#include <iostream>
#include <string>
using namespace std;
//关于cin cin.getline cin.get getline gets getchar 的用法实例
void main(int argc, char* argv[])
{
//1、cin>>
//method one, 也就是最常用的方法 输入一个数字
:" << endl;
int a,b;
cout << "input two integer:" << endl;
cin >> a >> b;
cout << "SUM =" << a + b << "\n" << endl;
//method two,输入一个字符串,遇到“空格 回车 Tab”都结束
:" << endl;
char array[10];
cout << "input a char array:" << endl;
cin >> array;
cout << array << "\n" << endl;
//2、cin.get()
//one cin.get(字符变量名) 可以用来接收字符
cout << "Test cin.get(字符变量名):" << endl;
char ch;
char cch;
cout << "Input a char:" << endl;
ch = cin.get(); //把之前输入的回车符号滤去
cch = cin.get(); //or cin.get(ch);
cout << cch << "\n" << endl;
//two cin.get(字符数组,接收的字符数) 用来接收一行字符串可以接收空格
cout << "Test cin.get(字符数组,接收的字符数):" << endl;
char array1[20];
cout << "Input a char array:" << endl;
ch = cin.get(); //把之前输入的回车符号滤去
cin.get(array1,10);
cout << array1 << "\n" << endl;
//注:cin.get(无参数)主要用来舍弃输入流中不需要的字符 或者舍弃回车
//从而弥补了cin.get(字符数组,接收的字符数)的不足
//3、cin.getline(cin,str) 接收一个字符串 可以接收空格
cout << "Test cin.getline() 的用法:" << endl;
char array2[20];
cout << "Input a char array:" << endl;
ch = cin.get(); //把之前输入的回车符号滤去
cin.getline(array2,20);
cout << array2 << "\n" << endl;
//实际上cin.get(字符数组,接收的字符数) 和cin.getline(字符数组,接收的字符数)
//有三个参数cin.getline(字符数组,接收字符数,结束字符) 第三个参数默认是'\0'
//多维数组中也经常用到cin.getline(字符数组,接收的字符数)的用法
cout << "cin.get(字符数组,接收的字符数) is used in multidimensional array:" << endl;
char array3[3][10];
for (int i = 0;i < 3;i ++)
{
cout << "请输入第" << i+1 << "行的字符串:" << endl;
cin.getline(array3[i],10);
}
for (int j = 0;j < 3;j ++)
{
cout << "第" << j+1 << "行:" << array3[j] << endl;
}
//4、getline(cin,str)的用法 接收一个可以包含空格的字符串(这儿是string类型的) 需要包含头文件#include <string>
//getline(cin,str)是string流不是i/o流
cout << "Test getline(cin,str):" << endl;
string str;
cout << "Input a string:" << endl;
//ch = cin.get(); //把之前输入的回车符号滤去
getline(cin,str);
cout << str << "\n" << endl;
//5、gets(char *) 接收一个可以包含空格的字符串 需要包含头文件#include <string>
cout << "Test gets(char *)的用法" << endl;
char array4[20];
cout << "input a char array:" << endl;
ch = cin.get(); //把之前输入的回车符号滤去
gets(array4);
//The gets function reads a line from the standard input stream stdin and stores it in buffer.
//The line consists of all characters up to and including the first newline character ('\n').
//gets then replaces the newline character with a null character ('\0') before returning the line
cout << array4 << "\n" << endl;
//gets(char *)也可以用在多维数组里面 跟cin.getline()用法类似
//6、getchar(无参数) 接收一个字符 需要包含头文件#include <string>
cout << "Test getchar(无参数)的用法:" << endl;
char ch1;
cout << "input a char:" << endl;
ch1 = getchar(); // 不能写成getchar(ch1);
cout << ch1 << "\n" << endl;
//getchar()是C的函数 C++是兼容C 所以也可以使用 但尽量不用或少用
}
C++语言中cin cin.getline cin.get getline gets getchar 的用法实例的更多相关文章
- java语言中public、private、protected三个关键字的用法,重写和重载的区别。
java语言中public.private.protected三个关键字的用法,重写和重载的区别. 解答: 作用域 当前类 同包 子类 其它 public √ √ √ √ protected √ √ ...
- C语言中memset(void *s, char ch,unsigned n)用的用法
将指针s所指的内存空间中前n为重置为字符c 程序例: #include <string.h> #include <stdio.h> #include <memory.h& ...
- c++中 cin、cin.get()、cin.getline()、cin.getchar()的区别
①cin>>:无法接收空格.Tap键且以空格.Tap键.回车符为分隔符: ②cin.get( ):可以接收空格.Tap键且以回车符为结束符: 一:可输入单个字符 格式: char ch; ...
- C++中cin.get(),cin.getline(),cin>>,gets(),cin.clear()使用总结
1.cin.get() 实质:类istream所定义对象cin的重载成员函数 用于读取单字符 istream& get(char&) int get(void) 用于读取字符 ...
- cin详解(cin.get()、cin.getline()、cin.clear()、cin.sync())
在C中,输入输出用scanf和printf,在输入数据的同时还需说明数据的类型,如果输入数据较多,那就很麻烦,而C++中也有相似的东西cin和cout,它们来自C++的一个名叫" iostr ...
- [原创]cin、cin.get()、cin.getline()、getline()、gets()、getchar()的区别
这几个输入函数经常搞不清具体特点和用法,这里稍作总结 一.cin>> 1.最基本用法,输入一个变量值 2.输入字符串,遇“空格”.“TAB”.“回车”结束,比如输入“hello world ...
- C++中关于cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
1.cin>> 用法1:最基本,也是最常用的用法,输入一个数字: 注意:>> 是会过滤掉不可见的字符(如 空格 回车,TAB 等) cin>>noskipws> ...
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法----细节决定成败 (sort用法)
C++中cin.cin.get().cin.getline().getline().gets()等函数的用法 学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有 ...
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行) 1.cin 2.cin.get ...
随机推荐
- shell控制流结构笔记
man test 可以看见这些 比较符号:-lt小于 -le小于等于 -gt大于 -ge大于等于 -ne不等于 -eq等于 < 小于(需要双 ...
- SqlSever基础 select 用+号连接两个字符串
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- Linux新手学堂 Crontab命令的语法
crontab 命令的用途就是:提交.编辑.列出或除去 cron 作业. 语法 crontab [ -e [UserName] | -l [UserName] | -r [UserName] | -v ...
- Cheatsheet: 2015.02.01 ~ 02.28
Other API Best Practices: API Management Rewriting History with Git Rebase .NET Announcing Microsoft ...
- 样式表中的 element.style样式如何修改
我们在写前面 web样式的时候,会发现有些时候,我们怎么修改 style里面的值,页面上的样式都不会修改,当你用工具查看时,会发现里面会有 element.style的值,这个值还找不到是在哪里出现的 ...
- Python使用requirements.txt安装类库
摘要:我们为何要应用requirements.txt呢? 首要应用目标: 任何 运用顺序 平常 须要设置装置 所需并 依附 一组类库去知足 事情请求 . 请求 文件 是 指定 战 一次性 装置 包 的 ...
- 深度信任网络的快速学习算法(Hinton的论文)
也没啥原创,就是在学习深度学习的过程中丰富一下我的博客,嘿嘿. 不喜勿喷! Hinton是深度学习方面的大牛,跟着大牛走一般不会错吧-- 来源:A fast learning algorithm fo ...
- [SAP ABAP开发技术总结]SD销售订单定价过程
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [SAP ABAP开发技术总结]列表屏幕
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Codeforces Round #257 (Div. 2) A题
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...