C++ - 无法对齐输出

标签 c++ formatting output

我有一个函数可以遍历一些并行数组并在循环控制变量中打印内容。我有另一个在我的程序中使用的,用于在循环控制变量的值处打印与 ID 关联的名称。这是两个函数

void PrintName(int ID)
{
    switch (ID)
    {
    case 0: cout << "computer(s)";
        break;
    case 1: cout << "pencil(s)";
        break;
    case 2: cout << "pen(s)";
        break;
    case 3: cout << "book(s)";
        break;
    case 4: cout << "beer(s)";
        break;
    case 5: cout << "ruler(s)";
        break;
    case 6: cout << "stereo(s)";
        break;
    case 7: cout << "refrigerator(s)";
        break;
    case 8: cout << "desk(s)";
        break;
    case 9: cout << "backpack(s)";
        break;
    }
}

void PrintSummary(InvIDList invIDs, ItemQuantity itemQuant, int totalWeight, int totalPurch, int budget)
{
    cout << "ID      NAME      QUANTITY" <<endl;
    cout << "--------------------------" <<endl;
    for(int i = 0; i < 10; i++){
        cout << invIDs[i];
        PrintName(i);
        cout << itemQuant[i] <<endl;
    }
    cout << "\n\nTotal Amount Spent: $" << totalPurch <<endl;
    cout << "Amount of Budget Remaining: $" << budget <<endl;
    cout << "Total Weight of Items Purchased: " << totalWeight <<endl;
}

我希望输出在 IDNAMEQUANTITY 下很好地对齐,ID 在最左边, QUANTITY 在最右边,NAME 居中。不幸的是,经过数小时的修改代码,我无法弄清楚如何实现这一点,主要是让 QUANTITY 一直向右对齐。

最佳答案

您不想使用 PrintName 函数,而是希望返回一个字符串对象以与 std::cout 一起使用。为了正确对齐列,您可能需要查看 <iomanip> header ,尤其是 std::setw(n) 操纵器。

这是一个示例,请注意,当我将列宽设置为 16 个字符时,您应该做的是计算每个类别的最大字符数并相应地更改宽度。这样做的好处是您可以为每列指定不同的宽度。

string ReturnName(int ID)
{
    switch (ID)
    {
    case 0: return "computer(s)";
        break;
    case 1: return "pencil(s)";
        break;
    case 2: return "pen(s)";
        break;
    case 3: return "book(s)";
        break;
    case 4: return "beer(s)";
        break;
    case 5: return "ruler(s)";
        break;
    case 6: return "stereo(s)";
        break;
    case 7: return "refrigerator(s)";
        break;
    case 8: return "desk(s)";
        break;
    case 9: return "backpack(s)";
        break;
    }
}

void PrintSummary(InvIDList invIDs, ItemQuantity itemQuant, int totalWeight, int totalPurch, int budget)
{
cout << left << setw(16) << "ID";
cout << left << setw(16) << "NAME";
cout << left << setw(16) << "QUANTITY" << endl;
cout << "--------------------------------------" << endl;
for(int i = 0; i < 10; i++){

    cout << left << setw(16) << invIDs[i];
    cout << left << setw(16) << ReturnName(i);
    cout << left << setw(16) << itemQuant[i] << endl;
}
cout << "\n\nTotal Amount Spent: $" << totalPurch <<endl;
cout << "Amount of Budget Remaining: $" << budget <<endl;
cout << "Total Weight of Items Purchased: " << totalWeight <<endl;
}

关于C++ - 无法对齐输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22945365/

相关文章:

vb.net - 在 String.Format 中插入格式化字符?

Python 写入缓冲区而不是文件

string - 如何强制 Text::CSV 将数字存储为文本?

c# - 如何在 sql server 中存储 Microsoft Word 文档的格式化片段

python - 如何在Python中使用numpy.savetxt居中对齐不同长度的列?

c++ - For循环在输出时未显示正确的计数,在C++中显示的数字过高

c++ - qt c++ 中的这个语句是做什么的?

c++ - C++ 中的流类型,如何从 IstringStream 中读取?

c++ - asio 周期性定时器

c++ - 需要帮助解决 LNK1104 : cannot open file 'opencv_core231.obj' when building an opencv helloworld with vs2010