help with setw and width functions!

J

Jess

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

Pete Becker

Jess said:
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)
 
J

Jess

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

jk

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),,,,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top