0

I am trying to pass 5th element of an array(Products[]) of class product to another function. The goal is to update the information of the element Product[5]. Everything seems to work fine except information of Product[5] variable is not updating.

Update: Problem solved by removing while(!EOF), thanks to Remy Lebeau.


The relevant part of Class:

class product
{
private:
    float Wholesale_Price = 0;
    float Retail_Price = 0;
    string Supplier = "N/A";
    string Note = "N/A";
    int Stock_Amount = 0;
public:
    string Name="N/A";

    void UpdateRetailPrice(float New_Retail_Price)
    {
        Retail_Price = New_Retail_Price;
    }
    void UpdateProductAmount(int New_Stock_Amount)
    {
        Stock_Amount = New_Stock_Amount;
    }
    void UpdateWholesale_price(float New_Wholesale_Price)
    {
        Wholesale_Price = New_Wholesale_Price;
    }
};

The relevant part of function:

void ReadProductFromFile(product* Products)
{
    string Name, Supplier, Note, Wholesale_Price, Retail_Price, Stock_Amount;//wholesale price, stock amount,price are in string so that
//it becomes easy to use getline(). Use stoi() later for turning string to int.

    ifstream ReadProductFromFile;
    ReadProductFromFile.open("product.txt");

    if (!ReadProductFromFile)
    {
        perror("Product File failed to open");
        return;
    }

    while(!EOF)
        {
        /*read product info txt file------>*/
        getline(ReadProductFromFile, Name); 
        getline(ReadProductFromFile, Wholesale_Price); 
        getline(ReadProductFromFile, Retail_Price); 
        getline(ReadProductFromFile, Stock_Amount);

        /*update product info---------->*/
        Products->Name = Name;
        Products->UpdateWholesale_price(stoi(Wholesale_Price));
        Products->UpdateProductAmount(stoi(Stock_Amount)); 
        Products->UpdateRetailPrice(stoi(Retail_Price));
        }
}

Relevant part of Main function:

int main(int argc, char const *argv[])
{
    product Products[10];
    ReadProductFromFile(Products+5);//is this the right way to send Products[5]? tried &(product[5]) but error
    return 0;
}

Input:

Bananas 
210 
270 
310
Miraz
  • 343
  • 3
  • 15
  • 3
    `Products+5` and `&Products[5]` result in the same address. But why are you passing the `product` *by pointer* instead of *by reference*? In any case, the only way the `product` object would not be updated is if `ReadProductFromFile.open()` fails, or `EOF` is true on the 1st loop iteration (and FYI, [checking an `istream`'s `EOF` this way is a logic bug](https://stackoverflow.com/questions/5605125/)). Why are you trying to read multiple products from the file into a single `product` object? That makes no sense. – Remy Lebeau Jun 22 '21 at 16:36
  • @RemyLebeau trying to update 4 members of product[5] class. And how to pass by reference? – Miraz Jun 22 '21 at 16:43
  • @RemyLebeau problem solved by removing `while(!EOF)`. – Miraz Jun 22 '21 at 16:47
  • 1
    You don't need a loop to read the members of 1 object. As for passing by reference, it would look like this: `void ReadProductFromFile(product& Product)`, and then you would replace `Products->xxx` with `Product.xxx`, and then you can call it like this: `ReadProductFromFile(*(Products+5));` or `ReadProductFromFile(Products[5]);` You should avoid pointers when you don't actually need them. – Remy Lebeau Jun 22 '21 at 16:47
  • @RemyLebeau Thanks, sir. I didn't know it could be this way. – Miraz Jun 22 '21 at 16:53
  • OT: You should move your initializations into a constructor. – Thomas Matthews Jun 22 '21 at 16:56

0 Answers0