formatting console output

I am trying to format my output data like this:
Basic Item          45
Basic Item Mk2      67
Fun Toy             20


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?
Last edited on
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.
THAT works for the right alignment, but now it's messing up my left alignment.
    Basic Item          45
Basic Item Mk2          67


any idea why?
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>

struct item
{
    std::string name;
    int value;
    const std::string& getName() const
    { return name; }
    int getValue() const
    { return value; }
};

using namespace std;
int main ()
{
    item armory[] = {{"Basic Item", 45}, {"Basic Item Mk2", 67}, {"Fun Toy", 20}};
    std::cout << std::left;
    for(const auto& it: armory)
        std::cout << std::setw(18) << it.getName() << it.getValue() << '\n';
}
Basic Item        45
Basic Item Mk2    67
Fun Toy           20
Last edited on
It works!

@MiiNiPaa thanks man, any idea though why std::right is default and not left?
Wouldn't it make more sense for it to be the opposite?
I would say number formatting.


000000
00000000 <---- Last digits don't match compared to

  000000
00000000
Last edited on
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?



:(
Show your code.
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.
1
2
3
4
5
std::cout << std::setw(27) << std::left <<"Equip Item: " << " Enter 'e' \n";
std::cout << std::setw(27) << "Un-equip Item: " << " Enter 'u' \n";
std::cout << std::setw(27) << "Use Buffs: " << " Enter 'b' \n";
std::cout << std::setw(27) << "Use Potions: " << " Enter 'p' \n\n";
std::cout << std::setw(27) << "Use attribute points: " << " Enter 'a' \n";




This is the output:
Equip Item:                       Enter 'e'
Un-equip Item:                    Enter 'u'
Use Buffs:                        Enter 'b'
Use Potions:                      Enter 'p'
Use attribute points:             Enter 'a'
Last edited on
According to what megatron 0 said std::right is supposed to be default. It seems correct and makes sense for it to be that way. As his example shows:

000000
00000000 <---- Last digits don't match compared to


  000000
00000000<---- Last digits now match


If this is correct, then why is my code not outputting numbers to the right?

1
2
3
4
5
std::cout << "Your Hero Stats: \n\n";
std::cout << std::left << std::setw(29) << std::setfill('-') << "Current Hit Points: " << std::right << CharacterPtr->getHp();
std::cout << std::left << std::setw(30) << "\nMax Hp: " << std::right << CharacterPtr->getMaxHp() ;
std::cout << std::left << std::setw(30) << "\nStrength: " << std::right << CharacterPtr->getStr() ;
std::cout << std::left << std::setw(30) << "\nDefense: " << std::right << CharacterPtr->getDef() ;


Output looks like this:
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:
1
2
std::cout.setf( std::ios::fixed, std:: ios::floatfield );
std::cout.precision(2);


Sorry this simple thing is a drag to me.
Last edited on
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
Topic archived. No new replies allowed.