代码之家  ›  专栏  ›  技术社区  ›  ongelo

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

  •  0
  • ongelo  · 技术社区  · 8 年前

    我正在做一个购物车项目,我想打印出用户输入,如以下输出。我不知道如何使用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
    ----------------------------------------
    

    这是我的主要观点。清洁石油产品

    #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 回复  |  直到 8 年前
        1
  •  0
  •   R Sahu    8 年前

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

    1. 第一列使用20个字符,并确保以左对齐文本输出。
    2. 使用 std::left std::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
    ----------------------------------------