如何从结构数组中提取数据?

时间:2016-03-06 19:14:14

标签: c++

我已成功完成我的代码,它正在填充数组,但当我尝试提取数据时,它告诉我“错误。表达式必须具有类类型”

void Inventory::fillInventory(char* buff, int len)
{
using namespace std;
int i = 0;
int upcNum = 0;
string itDesc = "";
string itPrice = "";
bool itTax = false;
do
{
    do
    {
        // assign upcNum
        if (buff[i] >= 48 && buff[i] <= 57)
        {
            string str = "";
            while (buff[i] != 32)
            {
                str += buff[i];
                i++;
            }
            upcNum = stoi(str, nullptr, 10);
        }
        // assign itDesc
        else if (buff[i] >= 97 && buff[i] <= 122)
        {
            string str = "";
            while (buff[i] != 32)
            {
                str += buff[i];
                i++;
            }
            itDesc = str;
        }
        // assign itPrice
        else if (buff[i] == 36)
        {
            string str = "";
            while (buff[i] != 32)
            {
                str += buff[i];
                i++;
            }
            itPrice = str;
        }
        // assign itTax
        else if (buff[i] == 78 || buff[i] == 84)
        {
            switch (buff[i])
            {
            case 78:
                itTax = false;
                break;
            case 84:
                itTax = true;
                break;
            }
        }
        i++;
    } while (buff[i] != 10 && i < len);


    // fill struct
    newItem = new Item;
    newItem->upc = upcNum;
    newItem->desc = itDesc;
    newItem->cost = itPrice;
    newItem->tax = itTax;

    if (inInventory < MAX_INV)
    {
        inventory[inInventory] = newItem;
    }
    else
    {
        cout << "Inventory is full..." << endl;
    }
    delete newItem;
} while (i < bufferLength);
int upcInt = inventory[0].upc; // this is my error
}

然而,尼克的解决方案似乎有效。

1 个答案:

答案 0 :(得分:1)

self._add_properties()

让我相信库存是一系列指向inventory[inInventory] = newItem; 的指针。如果是这样,您需要使用Item来访问元素的数据成员而不是inventory[0]->upc,因为每个元素都是指向inventory[0].upc的指针。