Manipulators

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 8
Reputation: sweeya is an unknown quantity at this point 
Solved Threads: 1
sweeya sweeya is offline Offline
Newbie Poster

Manipulators

 
0
  #1
Dec 19th, 2008
Hi i am trying to understand manipulators. Can someone explain what is happening here
  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

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Manipulators

 
0
  #2
Dec 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: sweeya is an unknown quantity at this point 
Solved Threads: 1
sweeya sweeya is offline Offline
Newbie Poster

Re: Manipulators

 
0
  #3
Dec 19th, 2008
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)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Manipulators

 
0
  #4
Dec 19th, 2008
Define your own manipulators, make a code simpler:
http://www.java2s.com/Tutorial/Cpp/0...anipulator.htm
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Manipulators

 
0
  #5
Dec 19th, 2008
Originally Posted by sweeya View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC