Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
struct category
{
int id;
char description[25];
float vatrate;
};
struct product
{
char productname[25];
float price;
int catid;
};
typedef struct product P;
typedef struct category C;
void welcome();
int basket(C array1[], P array2[]);
void till(int num,P array2[]);
int main()
{
int k = 0;
int i = 0;
int size;
const int limit = 10;
C cat[limit] = { { 0, "Hardware", 21 },
{ 1, "Electrical", 21 },
{ 2, "Appliance", 18 },
{ 3, "Food", 0 } };
P prod[limit];
welcome();
printf("Category Code\t\tCategory\t\t\t\tVAT%\n\n");
for(i=0;i<4;i++)
{
printf("%d\t\t\t%s\t\t\t\t%2.1f\n\n",cat[i].id,cat[i].description,cat[i].vatrate);
}
size=basket(cat,prod);
till(size,prod);
}
int basket(C array1[], P array2[])
{
int k, num=0, code;
printf ("How many items are being purchases\n");
scanf ("%d", &num);
for(k=0;k<num;k++)
{
printf("Please enter product name\n");
scanf ("%s", array2[k].productname);
printf ("Please enter a price of the product\n");
scanf("%f", &array2[k].price);
printf ("Please enter the category code\n");
scanf ("%d", &code);
array2[k].catid = array1[ code].vatrate;
}
if(code>3||code<0)//if the user types in an invalid category code it prints out this message
{
printf("No such code exists");
}
return num;
}
void till(int num,P array2[])
{
int q=0;
float vat;//the actual vat due is stored here
int cost;//this is the total cost
printf("Item\t%VAT\tActual Vat\tCost\n");
for(q=0;q<num;q++)//this for loop calculates the vat rate and prints out a table of information
{
vat=((array2[q].price/100)*array2[q].catid);//this calculates the vat due from each product
cost=(vat + array2[q].price);//this calculates the total cost of each product including VAT
printf("%s\t%2.0f\t%2.2f\t\t%3.2f\n",array2[q].productname,array2[q].catid,vat,cost);
}
}
void welcome()
{
printf ("\t\t\tWelcome\n\n");
}
Thats for last piece. worked a dream man. Just the receipt printing now to fix. Why does it keep giving me 0 vales for vat, actual vat and cost?