-1

my program is reading in 2 text files, one is going into an array and one is priming read normally. The one that is being read into an array has an item code, price, quantity, and item name. When the item code matches with the code on the other text document I need to get the price associated with it and cant figure out how.

while (!purchasesFile.eof())
{
    purchasesFile >> PurchaseItem >> purchaseQty;


    cout << purchaseNum << "  " << PurchaseItem << "  " << setw(4) << 
    purchaseQty << "  @ " << dollarSign << endl;

    int n = 0;  
        if (inventoryRec[n].itemCode != PurchaseItem)
        {
            inventoryRec[n+1];
        }
        else 
        {
            cout << inventoryRec[n].itemPrice << endl;
            inventoryRec[n+1];
        }

    if (PurchaseItem == inventoryRec[itemCount].itemCode)
    {
        inventoryRec[itemCount].itemOnHand - purchaseQty;
        purchaseAmount = inventoryRec[itemCount].itemPrice * purchaseQty;

        cout << purchaseAmount << "  " << 
       inventoryRec[itemCount].itemOnHand;

        purchaseCount++;
    }


    purchasesFile >> purchaseNum;
}
purchasesFile.close();
Conraw
  • 1
  • 2
    FYI: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/q/5605125/2486888) –  Apr 04 '18 at 04:13
  • Do you have a question? – Jive Dadson Apr 04 '18 at 04:24
  • When the item codes match I cant figure how to get the price that is in the same array spot as the item code. Current output https://gyazo.com/7296629f96adb493bfdad8027d06b1fb – Conraw Apr 04 '18 at 04:27
  • use std::vector. Life always goes better with std::vector – pm100 Apr 04 '18 at 15:46

1 Answers1

1

There are several statements in your code that do nothing:

inventoryRec[n+1];

inventoryRec[itemCount].itemOnHand - purchaseQty;

What you are looking for is probably something like the STL map

typedef struct inventory_item_t {
    inventory_item_t(const std::string& item_code, double price, int quantity) :
        item_code(item_code),
        price(price),
        quantity(quanity) { }

    std::string item_code;
    double price;
    int quantity;
} inventory_item_t;

typedef std::map<std::string, inventory_item_t> inventory_items_t;

inventory_items_t inventory_items;

inventory_items.insert(make_pair("item1", inventory_item_t("item1", 1.0, 1)));
inventory_items.insert(make_pair("item2", inventory_item_t("item2", 1.1, 2)));
inventory_items.insert(make_pair("item3", inventory_item_t("item3", 1.2, 3)));

inventory_items_t::iterator inventory_item = inventory_items.find("item1");

if(inventory_item != inventory_items.end()) {
    std::cout << "Inventory Item found - item_code: ["
              << inventory_item->first
              << "], price: ["
              << inventory_item->second.price
              << "]"
              << std::endl;
} else {
    std::cout << "Inventory Item not found" << std::endl;
}
Abdul Ahad
  • 742
  • 7
  • 16