C++快速入门---cin和cout输入的一些方法(2)

C++快速入门---cin输入的一些方法(2)

 

注意:

cin.ignore():忽略前7个字符

cin.getline():接收一个字符串

cin.get():获取一个字符

cin.peek():提取一个字符,不会改变输入流里面的数据

cin.gcount():计算提取到几个字符

cin.read():读取输入流

 

cout.precision():输出的时候,设定输出值以新的浮点数精读值显示

cout.width():获取当前输出长度

 

代码一:

#include <iostream>

using namespace std;

int main(void)
{
	char buf[20];
	
	cin.ignore(7);
	cin.getline(buf, 10);
	
	cout << buf << endl;
	
	return 0;
	
}

 

代码二:

#include <iostream>

using namespace std;

int main()
{
	char p;
	cout << "please input text:\n";
	
	while (cin.peek() != '\n')
	{
		p = cin.get();
		cout << p;
	}
	cout << endl;
	
	return 0;
}

 

代码三:

#include <iostream>

using namespace std;

int main(void)
{
	const int SIZE = 50;
	char buf[SIZE];
	
	cout << "please input text:";
	cin.read(buf, 20);
	
	cout << "recive the char number is:" << cin.gcount() << endl;
	
	cout << "the text of input is:";
	cout.write(buf, 20);
	cout << endl;
	
	return 0;
}

 

代码四:

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	double result = sqrt(3.0);
	
	cout << "3 平方保留小数点后0~9位,结果如下:\n" << endl;
	
	for (int i=0; i<=9; i++)
	{
		cout.precision(i);
		cout << result << endl;
	}
	
	cout << "当前的输出精度为:" << cout.precision() << endl;
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值