I need some help to build the simple code (LAB exercise):

The “main” of the C++ program attached to this assignment expects a class called “item.” This class
must have methods for “enter” and for “print.” Below is a description of what each method should do:
item.enter() should allow the user to enter:
? the item's name
? the item's price
? the item's discount as a percentage of its cost
? the item's category
Note that the discount percentage should not be more than 10% and that the category is limited to
“food,” “clothing” or “other.”
item.print() should print the following in the order specified, all on one line:
? the item's name
? the item's discount percentage
? the tax on the item
? the total cost of the item, taking into consideration its discount percentage and tax
Note that only items in the category “clothing” and “other” are taxed. Items in the category “food” are
not taxed.
Your “main” must look like this:
int main () {
item cart[3];
int i = 0;
while (i < 3) {
cart[i++].enter();
}
i = 0;
cout << "Item: Name price discount tax total" << endl;
while (i < 3) {
cart[i++].print();
}
return 0;
}

THX!

Dani AI

Generated

Below is a compact, ready-to-use item class that shows how to collect validated input and compute discount/tax. It responds to and expands on the gentle nudges from (read the forum rules) and (try it yourself — learn by doing). Key points shown: safe numeric input, case-insensitive category handling, and a single tax constant you can change to match your assignment.

The code uses std::getline for the name (so spaces work), loops to re-prompt on bad numeric input, converts category input to lowercase for comparison, and applies tax only where appropriate. Adjust TAX_RATE to the rate your instructor expects.

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <limits>

class item {
    std::string name;
    double price;
    double discount; // percent
    enum Category { FOOD, CLOTHING, OTHER } category;
    static const double TAX_RATE;
public:
    item() : price(0.0), discount(0.0), category(OTHER) {}
    void enter();
    void print() const;
};

const double item::TAX_RATE = 0.13; // change to match assignment/local tax

void item::enter() {
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Name: "; std::getline(std::cin, name);

    while (true) {
        std::cout << "Price: ";
        if (!(std::cin >> price) || price < 0) { std::cout << "Invalid price\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); continue; }
        break;
    }

    while (true) {
        std::cout << "Discount percent (0-10): ";
        if (!(std::cin >> discount) || discount < 0 || discount > 10) { std::cout << "Invalid discount\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); continue; }
        break;
    }

    std::string cat;
    while (true) {
        std::cout << "Category (food/clothing/other): ";
        if (!(std::cin >> cat)) { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); continue; }
        for (size_t i = 0; i < cat.size(); ++i) cat[i] = std::tolower(static_cast<unsigned char>(cat[i]));
        if (cat == "food") { category = FOOD; break; }
        if (cat == "clothing") { category = CLOTHING; break; }
        if (cat == "other") { category = OTHER; break; }
        std::cout << "Invalid category\n";
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

void item::print() const {
    double discAmt = price * discount / 100.0;
    double after = price - discAmt;
    double tax = (category == FOOD) ? 0.0 : after * TAX_RATE;
    double total = after + tax;
    std::cout << std::fixed << std::setprecision(2)
              << name << " " << price << " " << discount << "% " << tax << " " << total << "\n";
}

Notes: compile with any standard C++ compiler. If your driver main prints a header that differs from the fields above, tweak print() order/format accordingly. If mixing >> and getline, the ignore() calls prevent leftover-newline issues. Adjust TAX_RATE and output formatting to match assignment requirements.

Recommended Answers

All 2 Replies

Yes, you need to do it. You should also read some of the rules.

Try to do it by yourself... There are many sample programs in the internet to learn from.. Learn from others work, but dont just depend on it.. Have some fun... You can do it....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.