Bri
Bri

Reputation: 9

Having trouble with structures and arrays in my C program

***** CODE UPDATED WITH NEW QUESTION *****

I'm having trouble with building the 1st table from my list below of what I'm trying to output.

[This is a photo of how the output is SUPPOSED look][1] (notice the 1st table)

[This is a screenshot of what my below code is outputting][2]

I can't get the first table to descend by quantity of each item sold.


My objective:

I'm trying to create a C program to calculate the total sales of a company that collects data from 2 input files and outputs these 3 things:

1) Quantity of items sold listed in descending order of quantity of each item sold displayed in a table categorized with headers "Item Number", "Item Description" and "Quantity Sold".

2)Total sales of each item listed in descending order of total sales (total sales = quantity sold * unit price) displayed in a table categorized with headers "Item Number", "Item Description", "Quantity Sold", "Unit Price" and "Total Sales".

3)The total sales value (the sum of each total from #2 above)

Upvotes: 0

Views: 89

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409216

Lets take the prototype declaration

int readItemList(struct items[]);

Here you declare a function readItemList which returns an int, and as argument take a pointer to a struct item. Almost but not quite what you want.

What you want is for the function to take a pointer to struct Item (note upper-case I):

int readItemList(struct Item *items);

The second error is because you don't include <string.h>.


However those are just problems that the compiler detects, you have an even worse error that will lead to undefined behavior and most likely lead to a crash when you run the program: You use an uninitialized local variable.

In the main function you have

//Creating dynamic array of structure
struct Item *items;

The problem is that you don't actually create anything. You just pass the pointer, uninitialized (which means its value is indeterminate) to the readItemList function, and then readItemList treat it as an already allocated array. Nowhere do you allocate memory for this pointer.

The easiest solution is to make items an array in the main function:

struct Item items[200];

Unless you have more than 200 items in the file that would suffice, you might even make the array smaller.

Another solution is to actually dynamically allocate the memory for the items, either in main before passing it to the readItemList function. Or have the readItemList function allocate it, but then you need to change it to take a pointer to the variable, emulating pass by reference, and have readItemList allocate and reallocate as needed.

Upvotes: 2

Yağmur Oymak
Yağmur Oymak

Reputation: 477

In your prototypes, you are not declaring a function which takes an array of struct Item. struct items[] does not mean anything sensible here (there is no struct like struct items. You must use struct Item items[], like type identifier[]. Another example may be struct Item array_name[]. For the problem with strcmp, you need to include string.h. That's why it gives the implicit declaration warning.

Upvotes: 0

Related Questions