Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

iomanip not working as i expect

19 views
Skip to first unread message

Christopher Pisz

unread,
Jan 20, 2016, 12:44:50 PM1/20/16
to
It's been an eon since I had to use these manipulators. I am not getting
the output I expected. I am looking for a 2 digit month next to a 2
digit year.


#include <iostream>
#include <sstream>
#include <iomanip>

int main()
{
int month = 1;
int year = 2016;

std::ostringstream monthYearAsText;
monthYearAsText << std::setw(2) << std::setfill('0') << std::right
<< month
<< std::setw(2) << std::setfill('0') << std::right
<< year;

std::cout << monthYearAsText.str() << std::endl;

system("pause");

return 0;
}

Output:
012016

Expected Output:
0116

Where am I going wrong?


--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

Bo Persson

unread,
Jan 20, 2016, 1:06:43 PM1/20/16
to
On 2016-01-20 18:44, Christopher Pisz wrote:
> It's been an eon since I had to use these manipulators. I am not getting
> the output I expected. I am looking for a 2 digit month next to a 2
> digit year.
>
>
> #include <iostream>
> #include <sstream>
> #include <iomanip>
>
> int main()
> {
> int month = 1;
> int year = 2016;
>
> std::ostringstream monthYearAsText;
> monthYearAsText << std::setw(2) << std::setfill('0') << std::right
> << month
> << std::setw(2) << std::setfill('0') << std::right
> << year;
>
> std::cout << monthYearAsText.str() << std::endl;
>
> system("pause");
>
> return 0;
> }
>
> Output:
> 012016
>
> Expected Output:
> 0116
>
> Where am I going wrong?
>
>

The width (setw) is considered a minimum width, and the stream uses the
fill and right attributes to decide the padding up to this width.

However, if the output is already >= the requested width, it is just
output as is. Even if it is wider.


Bo Persson

Scott Lurndal

unread,
Jan 20, 2016, 1:14:01 PM1/20/16
to
Christopher Pisz <nos...@notanaddress.com> writes:

>Where am I going wrong?

You should be using snprintf :-)

char monthyearastext[5];
snprintf(monthyearastext, sizeof(monthyearastext), "%2.2u%2.2u", month, year);

or strftime if you're starting from a struct tm.

Barry Schwarz

unread,
Jan 20, 2016, 3:11:55 PM1/20/16
to
On Wed, 20 Jan 2016 18:13:46 GMT, sc...@slp53.sl.home (Scott Lurndal)
wrote:

>Christopher Pisz <nos...@notanaddress.com> writes:
>
>>Where am I going wrong?
>
> You should be using snprintf :-)
>
> char monthyearastext[5];
> snprintf(monthyearastext, sizeof(monthyearastext), "%2.2u%2.2u", month, year);

For conversion specifier u, both field width and precision specify
minimum values. Thus %2.2u will still convert year to a
four-character value.

However, since snprintf has been directed to output at most 4
characters, the last two characters of the converted year value will
be discarded. Thus, the resulting string from the above code will be
"0120" and not the desired "0116".

--
Remove del for email

Scott Lurndal

unread,
Jan 20, 2016, 3:59:41 PM1/20/16
to
Barry Schwarz <schw...@dqel.com> writes:
>On Wed, 20 Jan 2016 18:13:46 GMT, sc...@slp53.sl.home (Scott Lurndal)
>wrote:
>
>>Christopher Pisz <nos...@notanaddress.com> writes:
>>
>>>Where am I going wrong?
>>
>> You should be using snprintf :-)
>>
>> char monthyearastext[5];
>> snprintf(monthyearastext, sizeof(monthyearastext), "%2.2u%2.2u", month, year);
>
>For conversion specifier u, both field width and precision specify
>minimum values. Thus %2.2u will still convert year to a
>four-character value.
>

Odd. With %2.2s, it will only include two bytes from the string. I'd
expected symmetry for integers. C'est la vie.

strftime it is.

e.g.

#include <stdio.h>

unsigned long long main()
{
char str1[] = "abcd";
char str2[] = "efgh";

printf ("%2.2s%2.2s\n", str1, str2);
}
$ cc -o /tmp/d /tmp/d.c
$ /tmp/d
abef
$

Ben Bacarisse

unread,
Jan 20, 2016, 4:22:51 PM1/20/16
to
Christopher Pisz <nos...@notanaddress.com> writes:

> It's been an eon since I had to use these manipulators. I am not
> getting the output I expected. I am looking for a 2 digit month next
> to a 2 digit year.

<snip>
> int month = 1;
> int year = 2016;
>
> std::ostringstream monthYearAsText;
> monthYearAsText << std::setw(2) << std::setfill('0') << std::right
> << month
> << std::setw(2) << std::setfill('0') << std::right
> << year;

You could simply use year % 100 here.

<snip>
--
Ben.

Barry Schwarz

unread,
Jan 20, 2016, 6:01:19 PM1/20/16
to
On Wed, 20 Jan 2016 20:59:29 GMT, sc...@slp53.sl.home (Scott Lurndal)
wrote:

>Barry Schwarz <schw...@dqel.com> writes:
>>On Wed, 20 Jan 2016 18:13:46 GMT, sc...@slp53.sl.home (Scott Lurndal)
>>wrote:
>>
>>>Christopher Pisz <nos...@notanaddress.com> writes:
>>>
>>>>Where am I going wrong?
>>>
>>> You should be using snprintf :-)
>>>
>>> char monthyearastext[5];
>>> snprintf(monthyearastext, sizeof(monthyearastext), "%2.2u%2.2u", month, year);
>>
>>For conversion specifier u, both field width and precision specify
>>minimum values. Thus %2.2u will still convert year to a
>>four-character value.
>>
>
>Odd. With %2.2s, it will only include two bytes from the string. I'd
>expected symmetry for integers. C'est la vie.

For integer conversions, precision specifies the minimum number of
digits. For string "conversions," it specifies the maximum number of
characters. It also has other meanings for various floating point
conversions.

Richard

unread,
Jan 20, 2016, 6:27:06 PM1/20/16
to
[Please do not mail me a copy of your followup]

sl...@pacbell.net spake the secret code
<u7Qny.117728$Sn4.1...@fx44.iad> thusly:

>Christopher Pisz <nos...@notanaddress.com> writes:
>
>>Where am I going wrong?
>
> You should be using snprintf :-)

Yet your own example gets it wrong, which just serves to demonstrate
why using printf-style functions is error-prone.

Just the other day I found yet-another-occurrence of a printf-style
varargs function being passed an object instead of a primitive type
for a log message. The code compiled just fine, but of course results
in absolute garbage when executed.

I prefer mechanisms that fail at compile time when I write something
non-sensical.

(Yes, I know some more modern compilers can be configured to warn or
error at such things. Do you know why? Because it is so easy to use
this construct incorrectly that they attempt to compensate for it.
This again just serves to demonstrate why it should be avoided.)
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0 new messages