| | |
Manipulators
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 1
Hi i am trying to understand manipulators. Can someone explain what is happening here
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)
for (int i = 0; i < n; i++) { cout << setiosflags(ios::left) << setw(15) << item[i] << setw(8) << setprecision(0) << code[i] << setiosflags(ios::right) << setw(8) << setprecision(2) << setiosflags(ios::fixed | ios::showpoint) << cost[i] << endl; }
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)
dsjfh 102 32.32 jdshfjd 120 1212.00 dfdsfdsf 322 32.23
Last edited by sweeya; Dec 19th, 2008 at 4:16 am.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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.
Try using resetiosflags() manipulators to restore the various flags to their defaults before each line of output.
Define your own manipulators, make a code simpler:
http://www.java2s.com/Tutorial/Cpp/0...anipulator.htm
http://www.java2s.com/Tutorial/Cpp/0...anipulator.htm
![]() |
Similar Threads
- C++ homework (C++)
- Right Justified / decimal point alignment (C)
- the beginner needs help (C++)
- c++ arrays, help identify element place (C++)
- Need direction on how start this program (C++)
- Need direction on how start this programm (C++)
- The Calculator (C++)
- I need special stuff in my project like system("cls") (C++)
Other Threads in the C++ Forum
- Previous Thread: Error while compilation in the args of the function
- Next Thread: Items in combobox do not appear. (visual studio 6.0)
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






