C++ 21----- 用cin输入
来源:互联网 发布:javascript什么意思 编辑:程序博客网 时间:2024/05/20 06:25
1. 字符串的输入
#include <iostream>
using namespace std;
int main()
{
int x;
cin>>hex>>x;
cout<<x;
system("PAUSE");
return 0;
}
2. 字符串的输入问题
cin把空格当成分隔符来看待,当它看到一个空格活着换行符时,它就认为当前的输入已经完成,因此它会自动在空格处加入一个空字符作为字符串的结束标志。
#include <iostream>
using namespace std;
int main()
{
char ch;
ch=cin.get();
cout<<"ch:"<<ch<<endl;
return 0;
}
3. enter键和换行符的区别。。enter 和'\n'的ascii码是一样的。
//循环获取字符。程序代码如下:
/*
#include <iostream>
using namespace std;
int main()
{
char ch;
while ((ch=cin.get())!='\n')
{
cout<<ch;
}
cout<<"\n程序结束\n";
return 0;
}
*/
//输出回车键和"\n"的ASCII码值。程序代码如下:
/*
#include<iostream>
using namespace std;
int main()
{
int c;
c=int('\n');
cout<<c;
cout<<endl;
c=cin.get();
cout<<c;
cout<<endl;
return 0;
}
*/
//回车符"\r"覆盖开头的字符串。程序代码如下:
/*
#include<iostream>
using namespace std;
int main()
{
cout<<"hello\r"<<"give me";
return 0;
}
*/
//编译器不同,使用回车符"\r"产生的结果也不同。例程程序代码如下:
/*
#include<iostream>
using namespace std;
int main()
{
cout<<"hello\r";
return 0;
}
*/
//需要输入时刷新缓冲区。程序代码如下:
/*
#include<iostream>
using namespace std;
int main()
{
char ch[10];
cout<<"hello\r";
cin>>ch;
return 0;
}
*/
//循环获取字符。程序代码如下:
/*
#include <iostream>
using namespace std;
int main()
{
char ch;
while ((ch=cin.get())!='\n')
{
cout<<ch;
}
cout<<"程序结束\n";
return 0;
}
*/
//循环输出字符直到遇到字符s。程序代码如下:
/*
#include <iostream>
using namespace std;
int main()
{
char ch;
while ((ch=cin.get())!='s')
{
cout<<ch;
}
cout<<"程序结束\n";
return 0;
}
*/
//将get()换为cin>>。程序代码如下:
#include <iostream>
using namespace std;
int main()
{
char ch;
cin>>ch;
while (ch!='\n')
{
cout<<ch;
cin>>ch;
}
cout<<"程序结束\n";
return 0;
}
=========================================
假如我们希望在输入时跳过空格并不记录最后的enter键,使用cin。===cin可以忽略空格和enter键。避免对这两个
字符的判断。
//get()函数的级联。程序代码如下:
/*
#include <iostream>
using namespace std;
int main()
{
char a,b,c;
cin.get(a).get(b).get(c);
cout<<"a:"<<a<<"\nb:"<<b<<"\nc:"<<c<<endl;
return 0;
}
*/
//循环到文件尾结束。程序代码如下:
#include <iostream>
using namespace std;
int main()
{
char ch;
while (cin.get(ch))
{
cout<<ch;
}
cout<<"程序结束\n";
return 0;
}
//带两个参数的get()函数的使用方法。程序代码如下:
/*
#include <iostream>
using namespace std;
int main()
{
char ch[20];
cin.get(ch,20);
cout<<ch;
cout<<"程序结束\n";
return 0;
}
*/
//带两个参数的get()函数的缺点。程序代码如下:
/*
#include <iostream>
using namespace std;
int main()
{
char ch1[20];
char ch2[20];
cout<<"请输入第一串字符:";
cin.get(ch1,20);
cout<<"字符串1为:"<<ch1<<endl;
cout<<"请输入第二串字符:"<<endl;
cin.get(ch2,20);
cout<<"字符串2为:"<<ch2<<endl;
cout<<"程序结束\n";
return 0;
}
*/
//cin.ignore()的使用方法。程序代码如下:
#include <iostream>
using namespace std;
int main()
{
char ch1[20];
char ch2[20];
cout<<"请输入第一串字符:";
cin.get(ch1,20);
cout<<"字符串1为:"<<ch1<<endl;
cout<<"请输入第二串字符:";
cin.ignore();
cin.get(ch2,20);
cout<<"字符串2为:"<<ch2<<endl;
cout<<"程序结束\n";
return 0;
}
cin.ignore(a,ch)方法是从输入流(cin)中提取字符,提取的字符被忽略(ignore),不被使用。每抛弃一个字符,它都要计数和比较字符:如果计数值达到a或者被抛弃的字符是ch,则cin.ignore()函数执行终止;否则,它继续等待。它的一个常用功能就是用来清除以回车结束的输入缓冲区的内容,消除上一次输入对下一次输入的影响。比如可以这么用:cin.ignore(1024,'\n'),通常把第一个参数设置得足够大,这样实际上总是只有第二个参数'\n'起作用,所以这一句就是把回车(包括回车)之前的所以字符从输入缓冲(流)中清除出去。
5. 带三个参数的get()函数
//使用带3个参数的get()函数。程序代码如下:
/*
#include <iostream>
using namespace std;
int main()
{
char ch1[30];
char ch2[30];
cout<<"请输入第一串字符:";
cin.get(ch1,30,'s');
cout<<"字符串1为:"<<ch1<<endl;
cout<<"请输入第二串字符:";
cin.ignore();
cin.get(ch2,30);
cout<<"字符串2为:"<<ch2<<endl;
cout<<"程序结束\n";
return 0;
}
*/
3) 有3个参数的get函数
其调用形式为
cin.get(字符数组, 字符个数n, 终止字符)
或
cin.get(字符指针, 字符个数n, 终止字符)
其作用是从输入流中读取n-1个字符,赋给指定的字符数组(或字符指针指向的数组),如果在读取n-1个字符之前遇到指定的终止字符,则提前结束读取。如果读取成功则函数返回true(真),如失败(遇文件结束符) 则函数返回false(假)。
//抛弃缓冲区中的数据。程序代码如下:
/*
#include <iostream>
using namespace std;
int main()
{
char ch1[30];
char ch2[30];
cout<<"请输入第一串字符:";
cin.get (ch1,30,'s');
cout<<"字符串1为:"<<ch1<<endl;
cout<<"请输入第二串字符:";
cin.ignore(1024,'\n');
cin.get(ch2,30);
cout<<"字符串2为:"<<ch2<<endl;
cout<<"程序结束\n";
return 0;
}
*/
//清空并丢弃缓冲区中的数据还有一个方法如下所示。
//获得缓冲区的容量并丢弃该容量的数据。程序代码如下:
#include <iostream>
#include <limits>//为了使用numeric_limits
using namespace std;
int main()
{
char ch1[30];
char ch2[30];
cout<<"请输入第一串字符:";
cin.get (ch1,30,'s');
cout<<"字符串1为:"<<ch1<<endl;
cout<<"请输入第二串字符:";
cin.ignore( numeric_limits<streamsize>::max(),'\n');
//numeric_limits<streamsize>::max()返回缓冲区的大小
//ignore 函数在此将把输入缓冲区中的数据清空
cin.get (ch2,30);
cout<<"字符串2为:"<<ch2<<endl;
cout<<"程序结束\n";
return 0;
}
- C++ 21----- 用cin输入
- c++cin输入
- 【C++】输入流cin方法
- 用cin输入
- [C++] cin空白换行输入处理
- C++—— cin输入流详解
- 输入cin>>
- scanf/cin输入若干整数以回车结束,并存储,用C/C++实现
- 用 cin 输入整数死循环问题
- 学习C++——文本的输入cin
- 关于C++cin输入错误清空缓冲区
- C/C++学习笔记2 - cin深入分析(上) - cin输入操作处理(原创)
- C/C++学习笔记2 - cin深入分析(上) - cin输入操作处理
- c++用cin和getline实现输入回车结束输入
- C++输入cin详解
- C++输入cin详解
- C++输入cin详解
- C++ 输入cin详解
- css3 box-sizing属性
- hadoop实践(二)-hadoop和ssh配置
- 第十六周项目1-(2)-小玩文件
- 企业网站建设的作用与意义
- 预防discuz网站的cc攻击
- C++ 21----- 用cin输入
- 对某APP的逆向之旅(3)
- PRO2-Day3
- 将自定义python模块增加到类库搜索路径下面
- 深入浅出聊Unity3D优化:从Draw Calls到GC
- Androidstudio 设置永久行号
- UIWebView相关API学习
- linux 安装jdk 和 tomcat
- 理解 Objective-C Runtime