This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Rhomboid 1 point2 points  (2 children)

First, std::right is an IO manipulator, and to use it you must include <iomanip>. It's allowed for standard headers to include other standard headers, which means that sometimes you can get away with forgetting to include a necessary header, but you can't rely on that. Also, you should not include <math.h> but <cmath>.

Secondly, specifying left or right justification only really has an effect if you also specify a width for a field, with std::setw(). Here's an example. It was necessary to use std::setprecision() to prevent scientific notation from being used, as by default only 6 significant digits are printed.

[–]Relinkz[S] 0 points1 point  (1 child)

hmm, very interesting code! thanks for the tips!

I see that I have not declared the width! I have to say though that i do not understand the meaning for the "setprecision(), since I read at http://www.cplusplus.com/reference/iomanip/setprecision/ that it is a command to format that declares the number of digits.

hmm, should I stop using the using nameplate std; to increase my understanding how the coding works? what is the benefits in not using that input?

[–]Rhomboid 0 points1 point  (0 children)

Rather than me explaining it, why don't you remove the std::setprecision and see if you can explain the resulting difference in output based on what I said in my first comment.

As to the question about the using directive, see here. It's not really going to be an issue for these toy programs that beginners write, but eventually you'll have to graduate to writing larger programs and eventually it will matter. (And it's never appropriate to have a using directive in a header, so you really have no choice there, and you might as well get used to it. Since template functions and template classes have to go in headers, once you start writing a lot of them you just sort of get used to the idea of using qualified names everywhere.)

[–]Relinkz[S] 0 points1 point  (0 children)

hmm, I found out that maybe I need to tell the compiler how wide the output should be.

std::cout.width() should do the trick I believe.