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

cout.precision (i); // Output data with precision i

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

cout.width (i); // It is the width of the output string is i, if it is insufficient, it will be filled in with spaces.
cin.width (); /// The width of the input string

#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!

Published 29 original articles · liked 0 · visits 486

Guess you like

Origin blog.csdn.net/qq_43771959/article/details/104434539