I am writing a function that compares two structures. It compares the names and the quantity. The total cost is then the price times the quantity wanted. if the the item is not there then there is a printed error and if the quantity exceeds the available quanitity then prints an error message. if the the item name is there and the item quantity is available, then it prints the item, the quanitity, and the price. My outoput looks like this
This is my code:Quote:
There are 4 items in inventory.
There are 3 items in grocery list
Item: potatoes
does not exist
Item: 2-liter soda
does not exist
Item: pounds of hamburger does not exist
Item: done
does not exist
------------------------------
Total Cost: $0.00
Code:typedef struct{
char name[STRINGMAX];
int quantity;
}Grocery_List_Item;
typedef struct{
char name[STRINGMAX];
int quantity;
float price;
} InvItem;
typedef InvItem InvArr[ITEMMAX];
typedef Grocery_List_Item Grocery_Arr[ITEMMAX];
int availability(Grocery_Arr groc_list, InvArr inv_list);
int fill_grocery_list(Grocery_Arr groc_list);
int fill_inv(InvArr inv_list);
void empty_str(char name[]);
int main(){
InvArr inv_item;
Grocery_Arr want_item;
int num_InvItems, num_GrocItems;
num_InvItems = fill_inv(inv_item);
num_GrocItems = fill_grocery_list(want_item);
printf("There are %d items in inventory.\n",num_InvItems);
printf("There are %d items in the grocery list.\n",num_GrocItems);
printf("Purchasing:\n");
availability(want_item, inv_item);
return 0;
}
void empty_str(char name[]){
char *last = strrchr(name,'\n');
if(last != NULL)
*last = '\0';
}
int fill_inv(InvArr inv_list){
int count = 0;
char temparr[STRINGMAX + 1];
do{
scanf("%f", &inv_list[count].price);
scanf("%d", &inv_list[count].quantity);
fgets(temparr, STRINGMAX, stdin);
strcpy(inv_list[count].name, temparr);
empty_str(temparr);
}while(inv_list[count++].price !=0);
return (count - 2);
}
int fill_grocery_list(Grocery_Arr groc_list){
int count = 0;
char temparr[STRINGMAX + 1];
do{
scanf("%d",&groc_list[count].quantity);
fgets(temparr, STRINGMAX + 1, stdin);
strcpy(groc_list[count].name, temparr);
empty_str(temparr);
}while(groc_list[count++].quantity !=0);
return (count - 1);
}
int availability(Grocery_Arr groc_list, InvArr inv_list){
int match, count = 0;
float total = 0.0, new_price = 0;
do{
match = strcmp(groc_list[count].name, inv_list[count].name);
if((match == 0) && (groc_list[count].quantity <= inv_list[count].quantity)){
new_price += (groc_list[count].quantity * inv_list[count].price);
printf("%s %d $%f\n",groc_list[count].name,groc_list[count].quantity,new_price);
total += new_price;
}
else if((match == 0) && (groc_list[count].quantity > inv_list[count].quantity))
printf("Item: %s insufficient quantity\n",groc_list[count].name);
else
printf("Item: %s does not exist\n",groc_list[count].name);
}while(groc_list[count++].quantity !=0);
printf("-------------------------\n");
printf("Total Cost: $ %.2f\n",total);
return total;
}
