在列中格式化输出(由用户输入)

Formatting output (inputted by the user) in columns

我正在做一个购物车项目,我想像下面的输出那样打印出用户输入。我不知道如何使用 setW 和 right/left 关键字,当涉及到用户决定输出结果时。因此,当他们输入不同长度的输入时,例如 setW(20) 并不适用于所有输入。

Here is your order:
----------------------------------------------------------------
Name                     Unit_Price                  Quantity
T-shirt                    .99                       2

Sweater                    .99                       1

iphone_case                .5                        3

Towel                      .99                        5

The total charge is 6.42
----------------------------------------------------------------

这是显示函数:

ostream& operator <<(ostream& os, Item& source) {
    os << source.getName() << setw(18)
       << source.getPrice() << setw(20) << source.getQuantity() << endl;
    return os;
}

这是我得到的输出:

Here is your order:
----------------------------------------
NAME             PRICE            QUANTITY
tshirt           19.99               2
sweater           39.99               1
iphone_case            25.5               3
towel            9.99               5

The total price of the order is 206.42
----------------------------------------

这是我的 main.cpp

#include "ShoppingCart.h"
#include "Item.h"

#include <iostream>
#include <iomanip>
#include <string>

    using namespace std;


    int main()
    {
        cout << endl << endl << "Welcome to XXX SHOPPING CENTER" << endl;

        Item items[10];
        ShoppingCart<Item> cart;
        char userAnswer;

        cout << "Enter the item you selected as the following order:\nname unitPrice quantity\n"
             << "(Name can not contain any space. Otherwise errors happen!)" << endl;
        cin >> items[0];
        cart.add(items[0]);

        cout << "Want to continue? y/n" << endl;
        cin >> userAnswer;

        int index = 1;
        while(userAnswer == 'y' || userAnswer == 'Y') {

            cout << "Enter the next item:" << endl;
            cin >> items[index];
            cart.add(items[index]);
            cout << "Want to continue? y/n" << endl;
            cin >> userAnswer;
            index++;
        }


        // Display the summary of the orders
        cout << endl << "Here is your order:" << endl;
        // "--------------------------------------------------------------------"
        for(int i=0; i < 40; i++)
            cout << "-";

        cout << endl << "NAME" << setw(18) << "PRICE" << setw(20) << "QUANTITY" << endl;

        for(int i=0; i < cart.getCurrentSize(); i++) {
            cout << items[i];
        }

        cout << endl << "The total price of the order is " << cart.getTotalPrice() << endl;
        // "---------------------------------------------------------------------"
        for(int i=0; i < 40; i++)
            cout << "-";
        cout << endl;

        return 0;
}

你使用的策略对我来说没有意义。这是获得所需输出的一种方法:

  1. 第一列使用 20 个字符,并确保输出的文本左对齐。
  2. 第二列和第三列使用 10 个字符,并确保它们以右对齐文本输出。
  3. 使用std::leftstd::right控制文本对齐。

这是一个演示该想法的简化程序。

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

struct Item
{
   std::string name;
   double price;
   int quantity;
};

std::ostream& operator<<(std::ostream& out, Item const& item)
{
   // First column
   out << std::left << std::setw(20) << item.name;

   // Second and third columns
   out << std::right << std::setw(10) << item.price << std::setw(10) << item.quantity;

   return out;
}

void printLine(std::ostream& out)
{
   for(int i=0; i < 40; i++)
      out << "-";
   out << std::endl;
}

int main()
{
   std::vector<Item> items;
   items.push_back({"tshirt", 19.99, 2});
   items.push_back({"sweater", 39.99, 1});
   items.push_back({"iphone_case", 25.50, 3});
   items.push_back({"towel", 9.99, 5});

   printLine(std::cout);

   // First column
   std::cout << std::left << std::setw(20) << "NAME"

   // Second and third columns
   std::cout << std::right << std::setw(10) << "PRICE" << std::setw(10) << "QUANTITY" << std::endl;

   // The items
   for(int i=0; i < 4; i++) {
      std::cout << items[i] << std::endl;
   }

   printLine(std::cout);
}

输出:

----------------------------------------
NAME                     PRICE  QUANTITY
tshirt                   19.99         2
sweater                  39.99         1
iphone_case               25.5         3
towel                     9.99         5
----------------------------------------