Aggregate Initialisation

Lab C++ Intermediate 20 points

Define struct Item { string name; int qty; double price; }; and a vector<Item> initialised with braces: pen 10 1.5, book 2 12.0, bag 1 30.0. Read a minimum quantity and print the items with qty >= it and the total value of those items.

Input

One integer.

Output

The items, then total X.

Example

Input

2

Output

pen 10 1.50
book 2 12.00
total 39.00
Sample input available Sample output available
Editor
C++

Hints

{"pen", 10, 1.5} initialises the fields in order.

Solution explanation

Brace initialisation of structs and vectors keeps test data compact.

Show solution
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

struct Item {
    string name;
    int qty;
    double price;
};

int main() {
    vector<Item> items = {{"pen", 10, 1.5}, {"book", 2, 12.0}, {"bag", 1, 30.0}};
    int minQty;
    cin >> minQty;
    double total = 0;
    cout << fixed << setprecision(2);
    for (const auto &it : items) {
        if (it.qty >= minQty) {
            cout << it.name << " " << it.qty << " " << it.price << "\n";
            total += it.qty * it.price;
        }
    }
    cout << "total " << total << "\n";
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD