cout.precision() && cout.width()

cout.precision(i);//输出精度为i的数据

for example:
i=0 ---------.>2
i=1 --------->2.1
i=4 -----------.>2.1235

cout.width(i);//就是输出的字符串宽度为i,不足的会用空格补足
cin.width();///输入字符串宽度

#include<iostream>

using namespace std;
int main()
{
	int width = 4;
	char str[20];
	
	cout<<"please cout the :\n";
	cin.width(5);
	
	while(cin>>str)
	{
		cout.width(width++);
		cout<<str<<endl;
		cin.width(5);
	}
	
	return 0;
 } 

输出:
please cout the :
i love you so much!i like you!
i
love
you
so
much
!i
like
you!

发布了29 篇原创文章 · 获赞 0 · 访问量 486

猜你喜欢

转载自blog.csdn.net/qq_43771959/article/details/104434539