Currently, I am trying to create a program that takes inventory of a stores products. I have been able to create a function that allows the user to input new items in a struct array but when I attempt to print the values I get garbage values. (Please ignore the switch statements as the code is work in progress).
#include <stdio.h>
#include <stdlib.h>
#define MAX_INVENTORY_SIZE 100
typedef struct {
char item_Number[3];
char item_Name[20];
float item_Profit;
float latest_Price;
unsigned int stock;
unsigned int total_Sold;
struct InventoryItemType *next;
}InventoryItemType;
void MainMenu();
void displayInventory(InventoryItemType *(*));
void addItem(InventoryItemType, int i);
int main()
{
int i=1;
char selection;
int count=1;
InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE] ;
inventoryItems[0]=NULL;
while(1)
{
MainMenu();
scanf(" %c", &selection);
switch(selection)
{
case 'A' :
displayInventory(inventoryItems);
break;
case 'B' :
case 'C' :
inventoryItems[count]= (InventoryItemType*)malloc(sizeof(InventoryItemType));
addItem(*inventoryItems[count], count);
continue;
case 'D' :
case 'E' :
case 'F' :
case 'G' :
case 'H' :
default :
printf("Invalid\n" );
}
printf("Bottom of code\n");
system("pause");
}
}
void MainMenu()
{
printf("A. Display Inventory\n");
printf("B. Display Sales\n");
printf("C. Add Item\n");
printf("D. Remove Item\n");
printf("E. Enter Shipment\n");
printf("F. Update Sales\n");
printf("G. Sort\n");
printf("H. Exit\n");
printf("Make a selection\n");
}
void displayInventory(InventoryItemType *display)
{
int i;
for(i=0; i<MAX_INVENTORY_SIZE; i++)
{
printf("Name:%s\n", display[i].item_Name);
printf("Stock:%d\n", display[i].stock);
printf("Price:%.2f\n", display[i].latest_Price);
printf("Total Value:%.2f\n", (display[i].stock)*(display[i].latest_Price));
printf("\n");
}
}
void addItem(InventoryItemType display)
{
printf("\nEnter details of item \n\n");
printf("Enter Item Name: \n");
scanf("%s", display.item_Name);
printf("\nEnter Item no: \n");
scanf("%s", display.item_Number);
printf("\nEnter Stock: \n");
scanf("%d", &display.stock);
printf("\nPurchase Price: \n");
scanf("%f", &display.latest_Price);
}
Update
I attempted to apply all answers (with a series of trial and error) and came up with this code. I currently have the code properly taking in a new item and displaying that item, but it breaks down after adding more items(if I continue through the errors I get garbage display values). I am pretty certain it has to do with the way in which I am allocating memory via malloc. My goal is to use malloc to create the space for the item before initializing values.
Edited code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_INVENTORY_SIZE 100
typedef struct {
char item_Number[4];
char item_Name[20];
float item_Profit;
float latest_Price;
float selling_Price;
unsigned int stock;
unsigned int total_Sold;
}InventoryItemType;
void MainMenu();
void displayInventory(InventoryItemType *, int);
void displaySales(InventoryItemType *, int);
InventoryItemType *addItem(InventoryItemType *, int);
int main()
{
int i=0, item_count=0;
char selection;
InventoryItemType *inventoryItems[MAX_INVENTORY_SIZE];
while(1)
{
MainMenu();
scanf(" %c", &selection);
switch(selection)
{
case 'A' :
displayInventory(*inventoryItems, item_count);
break;
case 'B' :
displaySales(*inventoryItems, item_count);
break;
case 'C' :
inventoryItems[item_count]=(InventoryItemType*)malloc(sizeof(InventoryItemType));
inventoryItems[item_count]=addItem(inventoryItems[item_count],item_count);
item_count++;
continue;
case 'D' :
case 'E' :
case 'F' :
case 'G' :
case 'H' :
default :
printf("Invalid Entry\n" );
system("pause");
}
system("cls");
}
}
void MainMenu()
{
printf("A. Display Inventory\n");
printf("B. Display Sales\n");
printf("C. Add Item\n");
printf("D. Remove Item\n");
printf("E. Enter Shipment\n");
printf("F. Update Sales\n");
printf("G. Sort\n");
printf("H. Exit\n");
printf("Make a selection\n");
}
void displayInventory(InventoryItemType *display, int key)
{
if(display[0].item_Name == NULL)
{
printf("No stock");
system("pause");
}
else
{
int i;
for(i=0; i<key; i++)
{
printf("Item No.:%s\n", display[i].item_Number);
printf("Item Name:%s\n", display[i].item_Name);
printf("Item Stock:%d\n", display[i].stock);
printf("Item Purchased Price:%.2f\n", display[i].latest_Price);
printf("Total Value of Items:%.2f\n", (display[i].stock)*(display[i].latest_Price));
printf("\n");
system("pause");
}
}
}
void displaySales(InventoryItemType *display, int key)
{
int i;
float total_profit=0;
for(i=0; i<key; i++)
{
printf("Item No.:%s\n", display[i].item_Number);
printf("Item Name:%s\n", display[i].item_Name);
printf("Number of Item Sold:%d\n", display[i].total_Sold);
printf("Item Selling Price:%.2f\n", display[i].selling_Price);
printf("Total Profit from Item:%.2f\n", (display[i].selling_Price-display[i].latest_Price)*display[i].total_Sold);
total_profit=total_profit+((display[i].selling_Price-display[i].latest_Price)*display[i].selling_Price);
if(i==key)
printf("Total Over-all Profit:%.2f", total_profit);
system("pause");
}
}
InventoryItemType *addItem(InventoryItemType *change, int key)
{
InventoryItemType *current= (InventoryItemType*)malloc(sizeof(InventoryItemType));
printf("\nEnter details of item \n\n");
printf("Enter Item no: \n");
scanf("%s", current[key].item_Number);
printf("Enter Item Name: \n");
scanf("%s", current[key].item_Name);
printf("Enter Stock: \n");
scanf("%d", ¤t[key].stock);
printf("Enter Purchase Price: \n");
scanf("%f", ¤t[key].latest_Price);
current[key].selling_Price=(current[key].latest_Price)*1.5;
current[key].total_Sold=0;
change=current;
system("cls");
return change;
}