943,685 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2074
  • C++ RSS
Dec 19th, 2008
0

Manipulators

Expand Post »
Hi i am trying to understand manipulators. Can someone explain what is happening here
C++ Syntax (Toggle Plain Text)
  1. for (int i = 0; i < n; i++) {
  2. cout << setiosflags(ios::left) << setw(15) << item[i]
  3. << setw(8) << setprecision(0) << code[i]
  4. << setiosflags(ios::right) << setw(8) << setprecision(2)
  5. << setiosflags(ios::fixed | ios::showpoint) << cost[i]
  6. << endl;
  7. }

For the first time, i mean in the first iteration of the loop, it prints correctly but from the second iteration, the output always seems to be right justified. Why?

Here is the output of the above loop

C++ Syntax (Toggle Plain Text)
  1. dsjfh 102 32.32
  2. jdshfjd 120 1212.00
  3. dfdsfdsf 322 32.23
Last edited by sweeya; Dec 19th, 2008 at 4:16 am.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sweeya is offline Offline
8 posts
since Nov 2008
Dec 19th, 2008
0

Re: Manipulators

The short answer is that ios::left and ios::right (and some others) are not mutually exclusive, but your code reflects an assumption that they are. It is possible to have both set simultaneously and, of so, it is up to the implementation to decide which one (or a combination) applies.

Try using resetiosflags() manipulators to restore the various flags to their defaults before each line of output.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Dec 19th, 2008
0

Re: Manipulators

Hi,
Thanks, using resetiosflags worked. I was under the impression that they are mutually exclusive. Anyways, how is it working for the first one correctly?(just want to understand what is happening)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sweeya is offline Offline
8 posts
since Nov 2008
Dec 19th, 2008
0

Re: Manipulators

Define your own manipulators, make a code simpler:
http://www.java2s.com/Tutorial/Cpp/0...anipulator.htm
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 19th, 2008
0

Re: Manipulators

Click to Expand / Collapse  Quote originally posted by sweeya ...
Hi,
Thanks, using resetiosflags worked. I was under the impression that they are mutually exclusive. Anyways, how is it working for the first one correctly?(just want to understand what is happening)
On the first loop iteration, the stream flags are set to defaults. That is not true for subsequent iterations.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error while compilation in the args of the function
Next Thread in C++ Forum Timeline: Items in combobox do not appear. (visual studio 6.0)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC