第二十一章流 3用cin输入

//第二十一章流 3用cin输入
// 1 字符串的输入
/*#include <iostream>
using namespace std;
int main()
{
	int x;
	cin>>hex>>x;
	cout<<x;
	system("pause");
    return 0;
}*/

// 2 字符串的输入问题
/*#include <iostream>
using namespace std;
int main()
{
	char word[12];
	cin>>word;
	cout<<word<<endl;
    return 0;
}*/

// 3 get()函数
/*#include <iostream>
using namespace std;
int main()
{
	char ch;
	//ch = cin.get();
	//cout<<"ch:"<<ch<<endl;
    //循环获取字符串
	while( (ch=cin.get()) !='\n')
	{
	   cout<<ch;
	}
	cout<<"程序结束"<<endl;	
	return 0;
}*/

//输出Enter键和'\n'的ASCII码值
/*#include <iostream>
using namespace std;
int main()
{
	int c;
	c = int('\n');
	cout<<c<<endl;
	c = cin.get();
	cout<<c<<endl;

    return 0;
}*/
//回车符'\r'和换行符'\n'是不同的,它的ASCII码值是13,回车符的作用是回到该行的开头,但是并不住下移动一行

//回车符'\r'覆盖开头的字符串
/*#include <iostream>
using namespace std;
int main()
{
	//cout<<"hello\r"<<"give me";
    cout<<"hello\r"; //由于回车符而不是换行符,因此暂时存放在缓冲区中的字符有可能不会立即显示出来
	return 0;
}*/

//需要输入时新缓冲区
/*#include <iostream>
using namespace std;
int main()
{
	//char ch[10];
	//cout<<"hello\r";
	//cin>>ch;
    
	//char ch;
	//while((ch=cin.get()) !='s')
	//{
	   //cout<<ch;
	//}
	//cout<<"程序结束"<<endl;
	
	char ch;
	cin>>ch;
	while(ch!='\n')
	{
	   cout<<ch;
	   cin>>ch;
	}
	cout<<"程序结束"<<endl;
	return 0;
}*/

// 4 带字符引用参数的get()函数
/*#include <iostream>
using namespace std;
int main()
{
   char a, b, c;
   cin.get(a).get(b).get(c);
   //由于cin.get(a)返回一个cin对像,因此可以省略掉输入cin对像的过程,这样直接在后面加上成员运算符(.)即可
   cout<<"a:"<<a<<endl;
   cout<<"b:"<<b<<endl;
   cout<<"c:"<<c<<endl;
   return 0;
}*/
//输入缓冲和输出缓冲是两码事,在执行输入时会首先新输入缓冲中的数据并将数据写入程度中者设备,而输出则是遇到endl或者flush方才新输出缓冲区中的数据,哪里个缓冲区满就自动清空哪里个缓冲区的数据,在程序结束时会按照次序清空缓冲多中的数据,不要混为一谈

//循环到文件结束
/*#include <iostream>
using namespace std;
int main()
{
	char ch;
	while(cin.get(ch)){
	   cout<<ch;
	}
	cout<<"程序结束"<<endl;
    return 0;
}*/


// 5 带两个参数的get()函数
/*#include <iostream>
using namespace std;
int main()
{
	char ch[20];
	cin.get(ch,20);
	cout<<ch;
	cout<<"程序结束"<<endl;

    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.ignore(); //解决方法用cin.ignore()来丢弃缓冲区中的换行符'\n'
	cin.get(ch2,20);
	cout<<"字符串口2为"<<ch2<<endl;
	cout<<"程序结束"<<endl;
    return 0;
}*/

// 6 带3个参数的get()函数
//istream &get(char *buffer, streamsize num, char delim)
//读取字符到buffer直到已读入num-1个字符,或者碰到EOF或delim(delim直到下一次不会被读取)
/*#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(); 解决方法调用带两个参数ginore()
	
	//cin.ignore(1024,'\n');


	//第二种清空缓冲区的方法
	cin.ignore(numeric_limits<streamsize>::max(),'\n');
	//numeric_limits<streamsize>::max()返回缓冲区的大小
	//ignore函数在此将把输入缓冲区中的数据清空

	
	cin.get(ch2,30);
	cout<<"字符串口2为:"<<ch2<<endl;
	cout<<"程序结束"<<endl;
	
    return 0;
}*/

// 7 getline()函数
/*#include <iostream>
using namespace std;
int main()
{
	char ch1[30];
	char ch2[30];
	cout<<"请输入第一串字符:";
	cin.getline(ch1,30);
	cout<<"字符串1为:"<<ch1<<endl;
	cout<<"请输入第二串字符:";
	cin.getline(ch2,30);
	cout<<"字符串2为:"<<ch2<<endl;
	cout<<"程序结束"<<endl;
    return 0;
}*/
//清空缓冲区
/*#include <iostream>
using namespace std;
int main()
{
	char ch1[30];
	char ch2[30];
	cout<<"请输入第一串字符:";
	cin.getline(ch1,30,'\s');
	cout<<"字符串1为:"<<ch1<<endl;
	cout<<"请输入第二串字符:";
	cin.ignore(1024,'\n');
	cin.getline(ch2,30);
	cout<<"字符串2为:"<<ch2<<endl;
	cout<<"程序结束"<<endl;
    return 0;
}*/

// 8 read()函数 读取指定数目的字符,并将它们存储在指定的位置中
// 输出乱字符"烫"程序代码如下
/*#include <iostream>
using namespace std;
int main()
{
	//char ch[30]; 
	//解决乱码问题, 是因为read读取的数据没有字符串结束符
	char ch[30]={0};
	cout<<"请输入字符:";
	cin.read(ch,30);
	cout<<ch<<endl;
	cout<<"程序结束"<<endl;
}*/
//超出字符进入缓冲区
/*#include <iostream>
using namespace std;
int main()
{
	//char ch[30]; 
	//解决乱码问题, 是因为read读取的数据没有字符串结束符
	char ch1[30]={0};
	char ch2[30];
	cout<<"请输入第一串字符:";
	cin.read(ch1,5);
	cout<<"字符串1为:"<<ch1<<endl;
	cout<<"请输入第二串字符:";
	cin.ignore(1024,'\n');
	cin.getline(ch2,30);
	cout<<"第二串字符为:"<<ch2<<endl;
	cout<<"程序结束"<<endl;
}*/


// 9 gcount()函数 用于输入流,并返回上一次输入操作被读入的字符数目
/*#include <iostream>
using namespace std;
int main()
{
	char ch[30];
	//cin>>ch;
	cin.getline(ch,30);
	int i=cin.gcount();
	cout<<"输入缓冲区中共有"<<i<<"个字符"<<endl;
    return 0;
}*/

//10 peek()函数 用于输入流中,并返回在流中的下一个字符,如果处于文件的尾处,则返回EOF
/*#include <iostream>
using namespace std;
int main()
{
	char Peek;
	char ch[30];
	int i=0;
	while((Peek=cin.peek()) !='c' && Peek!='\n')
	{
		cin.get(ch[i++]);
	}
	ch[i] = '\0';
	cout<<ch;
    return 0;
}*/


// 11 putback()函数 它将一个字符插入到输入流中的字符串中,
/*#include <iostream>
using namespace std;
int main()
{
	char ch;
	while(cin.get(ch))
	{
		if(ch=='#'){
			cin.putback('$');
		}else{
		    cout<<ch;
		}
	}
    return 0;
}*/

  

posted @ 2012-10-01 13:59  简单--生活  阅读(355)  评论(0编辑  收藏  举报
简单--生活(CSDN)