0

I'm doing a college assignment, and for whatever reason the first displayed line of text doesn't line up to the following lines, despite it being from a for loop.

void displayInventory(string itemNames[], double itemCost[], int itemNoShip[MAX][2])
{
  cout << endl << left << setw(20)<< "Item Name" 
    << setw(10)<< "cost" 
    << setw(10)<< "No. Stock" 
    << right << setw(25)<< "Shipping (1-Yes 0-no)\n";
    for (int i = 0; i < MAX; i++)
    {
    
    cout << left << setw(20)<< itemNames[i] 
      << setw(10)<< setprecision(2) << fixed << itemCost[i] 
      << setw(10)<< itemNoShip[i][0] 
      << right << setw(25)<< itemNoShip[i][1]<< endl;
    }
}

This code will produce the following output, where the first line is one space over to the right, while the rest of the lines are the same.

enter image description here

it should be displaying the following data in an orderly fashion similar to how the bottom 5 outputs in the image are in line. Sorry I don't know how to format the text to be the same it is in the text file we were provided.

lavender    1.50    40  1

milled soap 1.50    10  1

coconut oil 3.25    8   0

toothpaste  8.45    25  1

silverware  1.50    40  1

dish soap   1.50    10  1
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Welcome to StackOverflow. The trick in getting good answers is having an [mcve] in the question... we don't see your data, for one. – DevSolar Apr 04 '22 at 23:01
  • Probably `itemNames[0]` has 21 characters in it. – Eljay Apr 04 '22 at 23:05
  • I just tested by raising the setw for itemNames[] to 25, the issue still remains. – andrew clayton Apr 04 '22 at 23:09
  • There's probably a bug in the program. Since we don't have a [mcve], your next option is [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Eljay Apr 04 '22 at 23:11
  • I guess I'll just see if i can catch the professors office hours, thank you for trying though – andrew clayton Apr 04 '22 at 23:13
  • It works for me: https://onlinegdb.com/ncy0-gNhp Have a look. – Jerry Jeremiah Apr 04 '22 at 23:24
  • @andrewclayton Please understand that turning "my application having a problem" into "an example *of the problem*" is an *essential* debugging skill, and most definitely something you *should* practice. This is not some arbitrary thing that this website demands. It is something that you will be doing over and over again in your professional carreer. Going "I'll just ask somebody else then" is not the solution. – DevSolar Apr 05 '22 at 06:32

0 Answers0