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

时间:2017-09-21 02:43:55

标签: c++ cout outline

我正在开发购物车项目,我想打印出如下输出的用户输入。我不知道如何使用setW和右/左关键字,当涉及到用户决定输出将是什么样时。因此,当他们输入长度不同的输入时,例如setW(20)对它们都不起作用。

Here is your order:
----------------------------------------------------------------
Name                     Unit_Price                  Quantity
T-shirt                    $19.99                       2

Sweater                    $39.99                       1

iphone_case                $25.5                        3

Towel                      $9.99                        5

The total charge is $206.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 个答案:

答案 0 :(得分:0)

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

  1. 对第一列使用20个字符,并确保使用左对齐文本输出。
  2. 对第二列和第三列使用10个字符,并确保使用右对齐文本输出它们。
  3. 使用std::leftstd::right来控制文字对齐。
  4. 这是一个展示这个想法的简化程序。

    #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
    ----------------------------------------
    
相关问题