0

I have this in main:

Product newProduct;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
newProduct.display();

in Product.cpp I have:

cout << "$" << basePrice << " - " 
     << name << " - " << cout.precision(1) << weight << " lbs\n";

but changing the precision to (1) in the .cpp changes the basePrice to (1) as well. How do I change the precision on different variables in the same cout? is there a way? or do I just place them in different cout's? Would that even work? why or why not?

Update when I try the second cout it is adding the number 2 to the end of my name variable. In other words I ended the first cout after the name variable. It is working but adds the number 2 to the end.

Dylan L.
  • 1,243
  • 2
  • 16
  • 35

1 Answers1

3

Use the std::setprecision manipulator instead:

cout << setprecision(2) << "$" << basePrice << " - " 
 << name << " - " << setprecision(1) << weight << " lbs\n";

The number 2 is the return value of cout.precision() function, which is the current precision value, that is being inserted into the stream and thus output.

Edit:

Ouch, forgot to add #include <iomanip>.

Edit 2:

For completeness, see this question of mine on why cout.precision() affects the whole stream when called in the middle.

Community
  • 1
  • 1
iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • Worked! so what is the difference between the two functions? – Dylan L. Feb 09 '16 at 06:15
  • `precision` is a member function of `cout`, while `setprecision()` is a so-called manipulator object (though a function as well). Apparently, you can use the latter inside the stream, while the former needs to be called between calls to `operator<<`. Also, the latter doesn't return a value, as opposed to the former. – iksemyonov Feb 09 '16 at 06:25
  • 2
    `setprecision` definitely returns something; otherwise, there would be a problem using it with the `<<` operator. What it returns is an object of a library-internal type for which there is an overload of the `<<` operator. That particular overload will set the precision of the stream and then return the stream. – rici Feb 09 '16 at 06:37
  • @rici, I've just asked a question on those details, you're welcome to chime in. I was basing on the "`unspecified`" return value here http://www.cplusplus.com/reference/iomanip/setprecision/, and in general, the semantics of this, not the exact details. Thank you for the correction! – iksemyonov Feb 09 '16 at 06:42
  • @rici does it happen like so: http://stackoverflow.com/questions/4633864/how-do-the-stream-manipulators-work Note: there it's a function that returns the stream directly, without an creating an additional object. Or is it undefined and implementation dependent? – iksemyonov Feb 09 '16 at 07:06
  • The standard does not specify the details of library-internal types. When you see `unspecified` used in that fashion, you should think "some internal object", rather than "nothing". – rici Feb 09 '16 at 07:10
  • `std::hex` is not called. It simply is, and the mechanism by which it is applied to a stream is specified. `std::setprecision(3)` is a function call, which must return something. The rest of the narrative is in my first comment. So, no, it is not quite the same mechanism. But it's similar. – rici Feb 09 '16 at 07:15
  • I see now. Wish I had this in a separate question, that would be more clear and I'd gladly accept your answer. – iksemyonov Feb 09 '16 at 07:17