setw 在循环中以不同方式输出一行
我正在做一项大学作业,无论出于何种原因,显示的第一行文本与以下几行不对齐,尽管它来自 for 循环。
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;
}
}
此代码将产生以下输出,其中第一行向右一个空格,而其余行相同。
它应该以类似于底部的有序方式显示以下数据图像中的 5 个输出排成一行。抱歉,我不知道如何将文本格式设置为与我们提供的文本文件中的文本格式相同。
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
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论