2017-09-21 8 views
0

私はショッピングカートプロジェクトで作業しており、次のようなユーザー入力を出力したいと考えています。私は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; 
} 

答えて

0
です

あなたが使用している戦略は意味を持たない私にはe。

  1. 最初の列に20文字を使用し、左揃えのテキストで出力されていることを確認してください。
  2. 2番目と3番目の列に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 
----------------------------------------