473,506 Members | 14,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iomanip - alignment

I can't seem to get the alignment to switch back and forth combined
with a set width. I tryed a search on this group and alot of googling,
but still can't deduce the problem.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
// using cout instead of ostream & operator << method, for example
purposes
// hardcoding values instead of class data for example purposes

cout << setiosflags(ios::fixed);

cout << left << setw(25) << "attribute name:";
cout << right << setw( 6) << 0.111;
cout << endl;

cout << left << setw(25) << "attribute name";
cout << right << setw( 6) << 0.123556;
cout << endl;

// etc etc

cout << resetiosflags(ios::fixed);

return 0;
}

The output seems to always be left aligned. I've also tryed using <<
setiosflags and << resetiosflags with the same results.

Can anyone clear this up for me? I hate to admit that I've forgotten
something so basic.

What is the lifetime of the differant iomanip members used here?
How are right and left alignments represented internally? i.e do you
need to reset left before using right and vica versa? or are they two
states of the same bit.

Oct 18 '07 #1
2 13686

"brekehan" wrote in message
I can't seem to get the alignment to switch back and forth combined
with a set width. I tryed a search on this group and alot of googling,
but still can't deduce the problem.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
// using cout instead of ostream & operator << method, for example
purposes
// hardcoding values instead of class data for example purposes

cout << setiosflags(ios::fixed);

cout << left << setw(25) << "attribute name:";
cout << right << setw( 6) << 0.111;
cout << endl;

cout << left << setw(25) << "attribute name";
cout << right << setw( 6) << 0.123556;
cout << endl;

// etc etc

cout << resetiosflags(ios::fixed);

return 0;
}

The output seems to always be left aligned. I've also tryed using <<
setiosflags and << resetiosflags with the same results.

Can anyone clear this up for me? I hate to admit that I've forgotten
something so basic.
For the floating point you also may use setprecision(3) for 3 decimals after
the point:
cout << right << setw(6) << setprecision(3) << 0.123;
Oct 18 '07 #2
On Oct 18, 11:04 pm, brekehan <cp...@austin.rr.comwrote:
I can't seem to get the alignment to switch back and forth combined
with a set width. I tryed a search on this group and alot of googling,
but still can't deduce the problem.
It works fine for me, both with g++ and with Sun CC. However...
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// using cout instead of ostream & operator << method, for example purposes
// hardcoding values instead of class data for example purposes
cout << setiosflags(ios::fixed);
I'm not sure that this is defined behavior. I'd normally use
std::cout << std::fixed ;
or
std::cout.setf( std::ios::fixed, std::ios::floatfield ) ;

(In a real program, of course, you'll almost always be using
custom manipulators, defined in accordance with the semantics of
the data being output.)
cout << left << setw(25) << "attribute name:";
cout << right << setw( 6) << 0.111;
Note that the setw here doesn't do anything, since you're
generating more than six characters anyway ("0.111000").
cout << endl;
cout << left << setw(25) << "attribute name";
cout << right << setw( 6) << 0.123556;
Same comment as above for the setw. This generates "0.123556".
cout << endl;
// etc etc
cout << resetiosflags(ios::fixed);
Again, this isn't guaranteed to do what you think. "std::cout
<< std::resetiosflags( ios::floatfield )" would do the trick,
"std::cout << std::defaultfloat" is probably more readable, but
normally, if the goal is to restore the previous context, you'll
read the previous context before changing it, typically by means
of some sort of RAII class. (Of course, if you're using custom
manipulators, you'll arrange for them to restore the original
state at the end of the full expression, so this won't be
necessary.)
return 0;

}
The output seems to always be left aligned.
Try putting markers around it, and I think you'll see what the
problem is. Something like:
std::cout << '|' << std::left << std::setw( 25) << "attr:" <<
'|' ;
std::cout << '|' << std::right << std::setw( 6 ) << 1.23 << '|' ;
This will show exactly how each field is being formatted.
I've also tryed using <<
setiosflags and << resetiosflags with the same results.
Can anyone clear this up for me? I hate to admit that I've forgotten
something so basic.
What is the lifetime of the differant iomanip members used here?
How are right and left alignments represented internally? i.e do you
need to reset left before using right and vica versa? or are they two
states of the same bit.
Alignment is a field in ios, consisting of at least 2 bits. To
assign to it, you have to do something like:

std::setf( std::ios::left, std::ios::alignfield ) ;

The second argument tells setf to reset the bits first. This is
exactly what std::left does, however, so there should be no
problem using the manipulator.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7023
by: deanfamily11 | last post by:
Ok, here's another problem I'm having. I've tried several different things, but I just can't get anything to be right justified. Any thoughts? #include <iostream> #include <iomanip>...
4
17675
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word...
67
10654
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
13
2971
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
4
8853
by: alternativa | last post by:
Hi, I'd like to obtain an output looking as follows: name number phone address Caroline 233 34234 White St. 12 Anna 929043 093284 Brown St. 325...
12
818
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: #include <stdlib.h> #define MAGIC_NUMBER 64 void *my_malloc (size_t n) { char *result = malloc (n + MAGIC_NUMBER);
10
2180
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
2
3774
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
2
20117
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.