467,922 Members | 1,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,922 developers. It's quick & easy.

help with setw and width functions!

Hello,

I'm trying to use setw and width functions to set the padding spaces
between numbers. The aim is to create two white spaces between two
double e.g. "1.1 1.2". I did:

streamsize s = cout.width(); // get the default width

cout << 1.1 << setw(2) << 1.2 << setw(s) << endl;

However, I didn't get any space at all, the output was simply
"1.11.2". Have I done something wrong?

Moreover, I had:

using std::width

but the compiler complained it and I had to remove "std". I thought
"width" is in std, is it not?

Thanks!

Mar 22 '07 #1
  • viewed: 1650
Share:
3 Replies
Jess wrote:
Hello,

I'm trying to use setw and width functions to set the padding spaces
between numbers. The aim is to create two white spaces between two
double e.g. "1.1 1.2". I did:

streamsize s = cout.width(); // get the default width

cout << 1.1 << setw(2) << 1.2 << setw(s) << endl;

However, I didn't get any space at all, the output was simply
"1.11.2". Have I done something wrong?

Moreover, I had:

using std::width

but the compiler complained it and I had to remove "std". I thought
"width" is in std, is it not?

Thanks!
If you want two spaces, put in two spaces:

cout << 1.1 << " " << 1.2 << '\n';

setw sets the minimum width for the next insertion. Since the 1.2
requires three characters, there is no padding needed to get it to at
least two characters.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Mar 22 '07 #2
So setw doesn't set the width of the gap between the current and next
input? I thought it meant the gap's distance. Thanks for letting me
know!

Mar 22 '07 #3
jk
On 22 Mar 2007 04:59:06 -0700, "Jess" <wd***@hotmail.comwrote:
>So setw doesn't set the width of the gap between the current and next
input? I thought it meant the gap's distance. Thanks for letting me
know!
try setw(5),,,,
Mar 22 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Alex Vinokur | last post: by
10 posts views Thread by Mathieu Malaterre | last post: by
1 post views Thread by dasilva109 | last post: by
7 posts views Thread by teddarr | last post: by
1 post views Thread by al2004 | last post: by
5 posts views Thread by Christian Johannes Charbula | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.