I want my first item to be aligned to the left, and the value of the items to be aligned to the right no matter what size characters the first item is, how do I do this? This is what I currently have but it's not working.
1 2 3 4 5
for(int i = 0; i < armory.size(); i++){ //looping through vector
std::cout << armory[i].getName(); //every item has different name
std::cout << std::right << std::setw(10) << armory[i].getValue() << std::endl;
//this still aligns it only ten spaces from where the name ends for some reason.
}
This is what comes out :
Basic Item 45
Basic Item Mk2 67
Fun Toy 20
I am terribly bad when it comes to outputting data on the console, some help please?
You are almost there. You just have setw in wrong place: std::cout << std::setw(15) << armory[i].getName() << armory[i].getValue() << '\n';
Avoid use of the flushing std::endl in loops, it can hamper perfomance.
You didn't pass std::right before that somewhere, do you? Try to do std::cout << std::left before that (and I think width of 15 is not enough, make it 20)
EDIT: Oh, std::right is default. Pass std::left before outputting anything then.
I absolutely do not understand this.
I am having trouble YET AGAIN with outputting something else the way I want to, can anyone enlighten me by sharing a link for dummies or by a good explanation?
I solved it after I posted it, however, I still feel iffy when formatting new things.
I was looking online and I saw some stuff posted on this same site that clarified things for me a bit such as std::setw(num) determines minimum size of output.
This is my code and it's outputting fine. Thanks for the response MiiNiPaa.
Current Hit Points: ---------20.00 <-
Max Hp: ---------------------20.00 <-
Strength: -------------------0
Defense: --------------------5.00 < ----- Notice how numbers are not aligned to the right?
By the way, just for information, earlier in the program just a few lines (like 6 lines) I use:
std::cout << std::left << std::setw(30) << "\nMax Hp: " Ouputs a properly aligned and filled line with width 0f 30.
<< std::right << CharacterPtr->getMaxHp() ; Ouputs a properly aligned and filled line with least possible width. Provide another setw() call before it and you will see the difference.
Yes.
MiiNiPaa, you are awesome. You did not just hand me the answer you told me how to solve the issue.
Thanks man. I guess the numbers were just conforming to the previously established minimum width correct? Once I created a new minimum width they all aligned properly to the right.
setw() works only on next output. All outputs after that would be outputted as if setw(0) was called. Some manipulators work to all output (like std::fixed). Some works only for next output. Consult reference to learn about behavior of specific manipulator: http://en.cppreference.com/w/cpp/io/manip