c++ Printing the correct number of decimal points with cout | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

c++ Printing the correct number of decimal points with cout

I want to make a format for the output of some code and I cant reach it, I tried many combinations and still I get it right only for the first line, after that I quess I don't know how to "end the area" of setprecision, but maybe I am wrong. Here is a simple code and the output that I want to achive: https://code.sololearn.com/cJb8BpysxtST/#cpp Maybe I'm doing something wrong or there is another function or manipulator that does what I want and I can't find it (yet). Study reference: http://www.cplusplus.com/reference/ios/

25th Oct 2017, 7:28 PM
derXred
derXred - avatar
8 Answers
+ 4
I am not sure what you wish to display as output. You wish to use appropriate precision for elements a[i][0], but want a default precision of 4 for the rest. So I have created a print function, to determine the input and format it accordingly. I have tried doing something, and let me know it this is what you wished to get as the output, or if this worked for you: https://code.sololearn.com/csvC2pbZYYzJ/#cpp
26th Oct 2017, 4:31 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
I have made some additions, and output is coming just like the one shown in the code - example for an output.
26th Oct 2017, 5:52 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
Welcome! www.cplusplus.com is the best resource available. And, do complete their tutorial if you haven't...
26th Oct 2017, 6:23 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Welcome ^_^ !
26th Oct 2017, 6:31 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
Really nice code :D but is not exactly what i want. The values have the correct number of decimal after the point, but i would really like to move the elements from the first column for a better appearance. I mean i will try to explain it again, because i know is kinda odd output. I can get your output or other types but not this one :) The first column number 1(1.002) from the first line in on a position, under this position is number 4(1234), under 4 is number 1(1.03) from line 3. Second column lets say is ok. The third column is easy to set it "all to the right" with a bigger value for setw(). But the problem remanins for the first column. I have changed the value of the element from the line 2 col 1 from 1111 into 1234 to make my idea more visible. output should be something like this: 1.002 2 0.0004 1234 1113 60.0010 1.03 4 320.5002 i will put a link because in the SL text editor playground the char space is safer then the comment section: https://code.sololearn.com/cbxzo4IW6OhY/#cpp I am still working on a solution, but i was sure what im looking for is already there somewhere and i dont know it. Probably the easy way is something like setting the setw() based on the decimal point DOT"." Thank you for the answer and again nice code :) i saw something in it that I like though and probably I will "steal" it hihi :)))
26th Oct 2017, 5:26 AM
derXred
derXred - avatar
+ 2
I think i found what i was looking for, i belive i should use flags :D i will read the documentation and i will come back with the code, if is the one :))
26th Oct 2017, 5:36 AM
derXred
derXred - avatar
+ 2
Yep :) you did it :D congrats! and thank you for the help. Looks like i have to read more docs that i was aware of :) You have some tips for a faster learning/reading?
26th Oct 2017, 6:21 AM
derXred
derXred - avatar
+ 2
Good point! I was reading more from the reference section and not from tutorials...i dont know why. Thank you again!
26th Oct 2017, 6:30 AM
derXred
derXred - avatar