I'm having trouble aligning some text with setw and the left alignment. I'm not sure if it has something to do with the fact that I'm printing from a function. The text within quotes aligns fine, but the output from the function aligns to the right and screws up the placement of the text after it. Here's my code:
You should only have one setw per line. Or rather, the problem is the second setw's don't do anything useful, but rather mess up your output.
PS: What you probably want to do is this:
No, because with only one setw function call, it will cut off all text after the first 20 characters. It's going to require a setw for each cout statement, but what I don't understand is why the output from my functions are floating to the right when I put a "left" statement in there.
I'm not saying one setw alltogether, but only one setw per line. Please just try that line that I posted and see if that does what you want and fix the other lines accordingly, of course I am not going to write the entire code for you.
And
No, because with only one setw function call, it will cut off all text after the first 20 characters.
no it doesn't. I think you got a wrong impression on what setw does.
And that is wrong, as I just told you earlier. setw is a manipulator that makes sure that the next thing that is being inserted into the stream is filled to at least the size of it's parameter. So what this: cout << setw(20) << left << "HP: "
does is to set the state of cout so that the alignment is left (which means simplified, what you write is on the left side and the fill characters will be on the right) and that the overall width of the next insertion is 20 characters. In this case, this is "HP: " - basically, this means it will insert "HP:" and 17 space characters.
You call setw after the function. If you want to make sure that 20 is the overall width of the entire string, convert the output to a string via a stringstream before making the output.