How to Format Output in C++ Using setw() and endl()

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Format Program
AI Thread Summary
To achieve properly formatted output in C++, using `setw()` and `endl()` is essential. The discussion highlights the importance of placing `endl()` strategically to ensure output appears on separate lines rather than all on one line. It is recommended to create an example of the desired output format to clarify where new lines are needed. Additionally, `setw()` only affects the width of the next output field, so understanding its application is crucial for consistent formatting. Proper use of these functions enhances the readability of console output in C++.
magnifik
Messages
350
Reaction score
0
resovledddddd
 
Last edited:
Physics news on Phys.org
Its a bit hard to see how you intend things to be lined up since your code seems to just prints everything on one line. Perhaps you should include some endl() somewhere? If you are in doubt where, make an example of how you would like the output to be and start figuring out on what "condition" you want a new line.

Also, if you use setw() to format your output you should note, that it sets the with for the next field being output. You can see a short description on [1].[1] http://www.cprogramming.com/tutorial/iomanip.html
 
Back
Top