C++-------标准输入流

#include <iostream>

using namespace std;


//标准输入输出:
/*
cin.get() //一次只能读取一个字符
cin.get(一个参数) //读一个字符
cin.get(两个参数) //可以读字符串
cin.getline()
cin.ignore()
cin.peek()
cin.putback()
*/
void test01()
{
	//cin.get() //一次只能读取一个字符
	char c = cin.get();
	cout << "c = " << c << endl;

	 c = cin.get();
	cout << "c = " << c << endl;
	 c = cin.get();
	cout << "c = " << c << endl;
	 c = cin.get();
	cout << "c = " << c << endl;
}
//当输入as时,此时缓冲区中的数据是:a,s,换行;打印出来的时:c=a,c=s,c= 
//再输入一个字符,最后的c= 才能显示出来;

void test02()
{
	//cin.get(两个参数) //可以读字符串
	char buf[1024];
	cin.get(buf, 1024);

	cout << buf << endl;
	//输入Hello world 输出 hello world;
}

void test03()
{
	//cin.get(两个参数) //可以读字符串
	char buf[1024];
	cin.get(buf, 1024);

	char c = cin.get();//如果cin.get(两个参数)没拿走换行符,则下面的不用第二次输入就能自动执行;

	if (c == '\n') {
		cout << "换行还在缓冲区" << endl;
	}
	else {
		cout << "换行不在缓冲区" << endl;
	}

	cout << buf << endl;
}
//test03的测试结果:cin.get读取字符串时,不会把换行符拿走,遗留在缓冲区;


//cin.getline()
void test04()
{
	char buf[1024];
	cin.getline(buf, 1024);
	//cin.getline()取走了换行符,且没有输出;
	//所以下面的程序需要多一次的输入才能执行;
	char c = cin.get();
	if (c == '\n') {
		cout << "换行还在缓冲区" << endl;
	}
	else {
		cout << "换行不在缓冲区" << endl;
	}
}

//cin.ignore()
void test05()
{
	cin.ignore();

	char c = cin.get();

	cout << "c= " << c << endl;

}
//输入as,输出s,因为忽略了字符a;
//cin.ignore(n)表示忽略了n个字符;

//cin.peek() 头盔
void test06()
{
	char c = cin.peek();

	cout << "c= " << c << endl;
	
	c = cin.peek(); 

	cout << "c= " << c << endl;
}
//输入as,输出c=a,c=a;
//输入as,偷看一眼a,输出a,然后放回缓冲区,缓冲区中还是as,再输出a;

//cin.putback() 放回
void test07()
{
	char c = cin.get();
	cin.putback(c);

	char buf[1024];
	cin.getline(buf, 1024);

	cout << buf << endl;
}

int main()
{
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	test06();
	test07();
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值