+ 2
Is there a way to set field width when using cout?
If I want to pad a series of integers that range from one to five digits and have them right justified. I can do this in C with printf (). How do I do this with cout?
2 Respuestas
+ 1
do{
cin>>test
}
while(test>99999 || test <0)
0
After a little research, here's the way to do it: You need to use a manipulator to set the field width. Unfortunately, SoloLearn doesn't mention these in there C++ tutorial.
/*
* width.cpp
*
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char* argv[]) {
cout << 5 << endl;
cout << 100 << endl;
cout << 134555 << endl;
cout << setw(10) << 5 << endl;
cout << setw(10) << 100 << endl;
cout << setw(10) << 134555 << endl;
return 0;
}
And the output for this is (running under cygwin on Windows 10):
$ ./width.exe
5
100
134555
5
100
134555
YMMV!
Thanks,
KO