• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Real quick question

RedArmy

Platinum Member
I need a 0 preceding my single digit integer values so I wanted to implement something like the %002d that printf uses but in terms of cout. I'm not that familiar with the extra syntax for cout since I normally stick to printf, so I just thought thought I would ask if there is a way, and if so, what it is. Like always any help is appreciated, thanks!
 
A lot of the stream format manipulators like width, precision, fill, etc. are in iomanip header.

Try something like this:

#include <iostream>
#include <iomanip>
using namespace std;

int main( int argc, char ** argv ) {

int magic = 333;

cout.width(4);
cout.fill('0');
cout << magic << endl;

return(0);

}
 
Originally posted by: QuixoticOne
A lot of the stream format manipulators like width, precision, fill, etc. are in iomanip header.

Try something like this:

#include <iostream>
#include <iomanip>
using namespace std;

int main( int argc, char ** argv ) {

int magic = 333;

cout.width(4);
cout.fill('0');
cout << magic << endl;

return(0);

}

Thanks for the quickly reply! I was actually just figuring out about those different manipulators before I checked back here and have been trying some of them out but I have a question regarding how to implement them. If I need to have the 0 coming before my single digit integer value do I need to use <<left<< anywhere? Most of the examples I've seen are for precision to the right of the value and I just need one '0' to the left.

I guess I'm just confused on the precision and direction. Thanks again
 
Originally posted by: RedArmy


Thanks for the quickly reply! I was actually just figuring out about those different manipulators before I checked back here and have been trying some of them out but I have a question regarding how to implement them. If I need to have the 0 coming before my single digit integer value do I need to use <<left<< anywhere? Most of the examples I've seen are for precision to the right of the value and I just need one '0' to the left.

I guess I'm just confused on the precision and direction. Thanks again

I think precision is mostly after the decimal point (to the right of it)
e.g. 0.12345678901234
whereas width is the total field width including the leading part to the left of the zero.

Typically fields will be right-aligned by default so the extra "width" will be filled on the left with your fill character which will be space or can be changed to '0' for leading zeroes.

Of course as you say, you can tell the field to be left-aligned, though, so if that's the case then you'll have padding to the right after the last digit of precision.

So yes you can use the left alignment in which case you'll still have to set the digits of precision to the right of the decimal, set the overall minimum field width attribute, and the fill character. As I recall there's a way to force "+" and "-" as well since the default is to print "-" but not "+".


 
Back
Top