main.c
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
#include "utils.h"
#include "file.h"
int main()
{
int i;
int value;//star location
//alocate the memory
for(i = 0; i < MAX_PRODUCT; i++)
{
product[i] = (struct goods *)malloc(sizeof(struct goods));
if (product[i] == NULL)
{
_setcursortype(_NOCURSOR);
clrscr();
printf("Not enough memory!");
getch();
goto out;
}
product[i]->status = FREE;
}
//save some products in memory
strcpy(product[0]->ProductName, "MILK");
product[0]->code = 123;
product[0]->price = 2;
product[0]->supply = 30;
product[0]->status = TAKEN;
strcpy(product[1]->ProductName, "JUICE");
product[1]->code = 121;
product[1]->price = 2.39;
product[1]->supply = 30;
product[1]->status = TAKEN;
strcpy(product[2]->ProductName, "CHOCOLATE");
product[2]->code = 567;
product[2]->price = 1.5;
product[2]->supply = 30;
product[2]->status = TAKEN;
strcpy(product[3]->ProductName, "APPLES");
product[3]->code = 852;
product[3]->price = 1.25;
product[3]->supply = 30;
product[3]->status = TAKEN;
do
{
value = Menu();
//turn on cursor
_setcursortype(_NORMALCURSOR);
switch(value)
{
case 0: EnterProduct();
break;
case 1: DeleteProduct();
break;
case 2: FindProduct();
break;
case 3: SellProduct();
break;
case 4: DisplayProducts();
break;
case 5: LoadDatabase();
break;
case 6: SaveDatabase();
break;
case 7: CheckSave(z);
break;
}
}while(value != 7);
//free the memory
for (i = 0 ; i < MAX_PRODUCT; i++)
{
free(product[i]);
}
out:;
return 0;
}
main.h
Code:
#ifndef MAIN_H
#define MAIN_H
#define MAX_PRODUCT 100 //maximum number of products
#define TAKEN 1 //prevent writing in memory location
#define FREE 0 //make memory location writeable
#define ARROW_UP 328
#define ARROW_DOWN 336
#define RETURN 13
void CheckSave(int r);
void SortProducts(void);
void DisplayProducts(void);
void FindProduct(void);
void SellProduct(void);
void EnterProduct(void);
int KeyPressed(void);
int Menu(void);
struct goods
{
char ProductName[15];
int code;
int supply;
int status;
float price;
}*product[MAX_PRODUCT];
int z = 1;//products saved?
char buf[BUFSIZ];
#endif //MAIN_H
delete.c
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
#include "utils.h"
void DeleteProduct(void)
{
int j = 0;
char option;
int code;
do
{
back:;
clrscr();
printf("DELETE PRODUCT\n\n");
printf("Code: ");
fgets (buf, sizeof buf, stdin);
if(sscanf (buf, "%d", &code) != 1)
{
fprintf (stderr, "Invalid input!");
getch();
goto back;
}
//find and delete a product
for(j = 0; j < MAX_PRODUCT; j++)
{
if(product[j]->code == code)
{
z = 0;
product[j]->code = 0;
product[j]->status = FREE;
printf("Product has been deleted.\n\n");
printf("Delete another product?(Y/N)\n");
option = getch();
break;
}
}
if(j == MAX_PRODUCT)
{
printf("Code %d doesn't belong to any product.\n", code);
printf("Delete another product?(Y/N)");
option = getch();
}
}while(option == 'Y' || option == 'y');
}
Here is my "combined" piece of code, I hope that is pasted correctly.
I know about conio, so please spare me - next time I promise there wont be any unstandared library