0

I try to print out each element in the struct array, but the format is not what I expect. I would like to print them out like the picture attached, so what am I supposed to use to print out the same format as shown in the picture.

image

struct Inventory {
    string description;   //Description of the part kept in the bin
    int total;            //Number of parts in the bin

};

struct Inventory bins[30];

void DisplayInventory(int & count){
    for (int i = 0; i < count; i++)
    {
        cout << bins[i].description << setw(18) << bins[i].total << endl;
    }
    cout << endl;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

0

You are using std::setw() incorrectly. Try something more like this:

cout << setw(18) << left << bins[i].description << setw(5) << right << bins[i].total << endl;

Online Demo

If you use std::setfill() with a different character for each output, you can more easily see how this works, eg:

cout << setw(18) << left << setfill('.') << bins[i].description << setw(5) << right << setfill('*') << bins[i].total << endl;

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770