Поиск в массиве струтур и добавление подходящих элементов в другой массив структур

Всем привет. Пользователь вводит коды товаров, которые желает купить. Пока вводит по порядку, то всё нормально. а когда какой захочет код, то выдаёт какой-то мусор. подскажите, пожалуйста, что не так.

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


class CashMachine
{
private:
    struct Product
    {
        int productCode;
        string productName;
        double price;
        int quantity;
    };
    struct Basket
    {
        int _productCode;
        string _productName;
        double _price;
        int _quantity;
    };
    string departmentName;
    double amountWithoutTax;
    double tax;
    double amountWithTax;
    int size;
    Product* products;
    Basket* basket;
public:
    CashMachine()
    {
        size = 5;
        products = new Product[size];
        basket = new Basket[size];
        departmentName = "Назва відділення";
    }
    void printDepartmentName()
    {
        cout << departmentName << endl;
    }
    void setProducts()
    {
        products[0].productCode = 111;
        products[0].productName = "Автомобіль";
        products[0].price = 2000000;
        products[0].quantity = 15;
        products[1].productCode = 222;
        products[1].productName = "Телефон";
        products[1].price = 20000;
        products[1].quantity = 150;
        products[2].productCode = 333;
        products[2].productName = "Телевізор";
        products[2].price = 60000;
        products[2].quantity = 70;
        products[3].productCode = 444;
        products[3].productName = "Плеєр";
        products[3].price = 10000;
        products[3].quantity = 200;

        products[4].productCode = 555;
        products[4].productName = "Комп'ютер";
        products[4].price = 80000;
        products[4].quantity = 30;
    }
    void printProducts()
    {
        cout << "Товари у наявності:" << endl;

        for (int i = 0; i < size; i++)
        {
            cout << setw(0) << products[i].productCode << setw(12) << products[i].productName << setw(10) << products[i].price << setw(10) << products[i].quantity << endl;
        }
    }
    void shopping()
    {
        int n;
        cout << "Укажіть покупки" << endl;

        
        for (int i = 0; i < size; i++)
        {
            cout << "Укажіть код товару: ";
            cin >> n;
            /*if (n != products[i].productCode)
            {
                cout << "Такого товару немає у наявності!!!" << endl;
                break;
            }*/
            if (n == products[i].productCode)
            {
                basket[i]._productCode = products[i].productCode;
                basket[i]._productName = products[i].productName;
            }
            cout << "Укажіть кількість товару: ";
            cin >> basket[i]._quantity;
            if (basket[i]._quantity > products[i].quantity)
            {
                cout << "Такої кількості немає на складі!!!" << endl;
                break;
            }
            
        }
    }
    double findAmountWithoutTax()
    {
        for (int i = 0; i < size; i++)
        {
            amountWithoutTax = products[i].price * basket[i]._quantity;
        }
        return amountWithoutTax;
    }
    double findTax()
    {
        for (int i = 0; i < size; i++)
        {
            tax = amountWithoutTax * 0.18;
        }
        return tax;
    }
    double findAmountWithTax()
    {
        for (int i = 0; i < size; i++)
        {
            amountWithTax = amountWithoutTax + tax;
        }
        return amountWithTax;
    }
    void pritBasket()
    {
        for (int i = 0; i < size; i++)
        {
            cout << basket[i]._productCode << setw(10) << basket[i]._productName << setw(10) << basket[i]._quantity << endl;
        }
    }
    
    
    




};
int main()
{
        SetConsoleCP(1251);
        SetConsoleOutputCP(1251);
        CashMachine a;
        a.printDepartmentName();
        a.setProducts();
        a.printProducts();
        a.shopping();
        a.pritBasket();
        
        return 0;

}

Ответы (0 шт):