-
problem formatting
I have a program that prints out inventory which is formatted so that its lined up on the right like so:
Code:
Strawberries
Watermelons
Bananas
using the following code:
Code:
for (iter=items.begin(); iter!=items.end(); iter++)
cout<<setw(30)<<right<<(*iter)<<endl;
What I want to do now is to add a grid reference to where the items are. The grid reference is to be written like (x,y) and I want to put it in front of the item when printing the list, but I want to keep the list lined up on the right 30 spaces over, but obviously:
Code:
for (iter=items.begin(); iter!=items.end(); iter++)
cout<<setw(30)<<right<<"("<<iter->x<<","<<iter->y<<") "<<(*iter)<<endl;
will just line up the "(" 30 spaces over. Is there an easy way to do this?
:confused:
-
I tried that code, and it seems to work except "right", which I've never heard of. If there is a problem, it may be in your items class.
-
easy no, impossible no. You will need to use a different set of flags/manipulators for each field. I don't have my reference with me but it might look something like this, where the dots and underscores are just space indicators for a given field
(1, 3)........._____watermelon
(2, 6).........________bananas
string coordinates = "(1, 3)";
string name = "watermelon";
cout.width(15);
cout << setf(ios::left) << corrdintaes;
cout.width(25);
cout << setf(ios::right) << name << cout << endl;
-
Oops, looks like I wasn't very clear. What I meant was to print out like:
Code:
(1,10) Strawberries
(3,4) Watermelons
(10,0) Bananas
I believe right and left are from the iomanip header, and it's the same as setf(ios::right), although I'm not sure if right and left work outside of MSVC++...
-
Nope. 'right' and 'left' work the same with Borland, as well.
The problem is that you must use setw() between each insertion operator. Convenient, eh?
For lack of a better way to put this, you may, very well, be stuck with the "brute force" method.
One possibility may be to "left-align" your coordinates while "right" aligning your fruit...if you'll pardon the expression. It may not give you the "cosmetics" you're looking for, but sometimes you have to kick a program in the teeth! :)
-Skipper
-
Thanks for the help! The only other thing I can think of is to try and convert it all into one string through the use of a function, but that may be more trouble than its worth...