I want to iterate over a product array.The pointer that I want to use is Order, yet I receive an error stating;
error: called object is not a function or function pointer
47 | if(orders->product_items(j).sku == products[t].sku)
| ^~~~~~
hw4.c:49:22: error: called object is not a function or function pointer
49 | TotalCost += (orders->product_items(j).quantity * products[t].price);
| ^~~~~~
This is an updated question.
These are the two files, I've downloaded.
Orders.H
struct product {
short sku;
char *name;
float price;
};
struct item {
short sku;
short quantity;
};
struct order
{
short order_id;
char *customer_name;
short num_products;
struct item *product_items;
};
struct order * read_orders(const char *filename, short *num_orders);
Orders.C
#include <inttypes.h>
#include "orders.h"
struct order * read_orders(const char *filename, short *num_orders)
{
int fd;
fd = open(filename, O_RDONLY);
int err = fd;
if (err == -1) {
return NULL;
}
read(fd, num_orders, sizeof(*num_orders));
struct order *orders = (struct order *)
calloc(*num_orders, sizeof(struct order));
int i;
for (i=0; i<*num_orders; i++) {
struct order *op = &orders[i];
read(fd, &op->order_id, sizeof(op->order_id));
uint32_t sz;
read(fd, &sz, sizeof(sz));
op->customer_name = (char *)malloc(sz+1);
read(fd, op->customer_name, sz);
op->customer_name[sz] = 0;
read(fd, &op->num_products, sizeof(op->num_products));
op->product_items = (struct item *)
calloc(op->num_products, sizeof(struct item));
int j;
for (j=0; j<op->num_products; j++) {
struct item *ip = &(op->product_items[j]);
read(fd, &ip->sku, sizeof(ip->sku));
read(fd, &ip->quantity, sizeof(ip->quantity));
}
}
close(fd);
return orders;
}
It's only this slight part of the code mentioned above, that's been troubling me. Other than that, the remainder of the code I believe should be fine. I would want assistance but not the answer. I also cannot make any modifications to this code before my first loop that iterates over the orders array.
to run the code; gcc -o hw4 hw4.c orders.c
What I have tried:
<pre>#include <stdio.h>
#include <stdlib.h>
#include "orders.h"
struct product products[] = {
{
1234, "Bread", 2.99
},
{
5678, "Milk", 4.19
},
{
9012, "Eggs", 3.49
},
{
3456, "Butter", 3.99
},
{
7890, "Juice", 2.49
},
{
2345, "Muffin", 1.49
}
};
int main()
{
short num_orders;
struct order *orders = read_orders("orders.db", &num_orders);
if (orders == NULL)
{
printf("Order database does not exist\n");
}
for (int i = 0; i < num_orders; i++)
{
float TotalCost = 0;
printf("Order ID: %hd", orders[i].order_id);
printf("Name: %s\n", orders[i].customer_name);
for (int j = 0; j < orders[i].num_products; j++)
{
for (int t = 0; t< 6; t++)
{
if(orders->product_items(j).sku == products[t].sku)
{
TotalCost += (orders->product_items(j).quantity * products[t].price);
printf("%s %hd %f/n", products[t].name, orders->product_items[j].quantity, products[t].price);
}
}
}
printf("Total: %f\n",TotalCost);
printf("\n");
}
}