0

So, I'm supposed to make a program that works like a shopping cart(add/remove products and total price) without editing the Main . My problem is that the Remove function or Total does not work properly. Any idea how to fix ? btw it looks like the Total is not working fine after Remove

#include <iostream>
#include <string.h>
#include <vector>
using namespace std;


struct Product {
public:
    string name;
    int price;
};

struct Cart {
public:
    vector<Product> product;

void Remove(string st1) {
    for (auto it = product.begin(); it != product.end(); it++) {
        if (it->name==st1) {
            product.erase(it);
        }
        
    }
}
void Add(Product* p) {
    for (int i = 0;i<5; i++) {
        product.push_back(p[i]);
    }
}

int Total() {
    int total_price = 0;
    for (auto it = product.begin(); it != product.end(); it++) {
        total_price += it->price;
    }
    return total_price;
}

};



int main()
{
    Cart Cos;
    Product* p = new Product[5];
    p[0].name = "shirt";
    p[0].price = 500;
    p[1].name = "jeans";
    p[1].price = 1000;
    p[2].name = "jacket";
    p[2].price = 1225;
    p[3].name = "tie";
    p[3].price = 210;
    p[4].name = "shoes";
    p[4].price = 900;
    Cos.Add(p);
    cout << Cos.Total(); // Till here it works fine 
    Cos.Remove("jacket");
    cout << Cos.Total();

    return 0;
}


Wr0ng
  • 1
  • 1

0 Answers0