Below is my code:
Code:
// Inventory Program
// What is the correct method of array of struct pointer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
void update();
void deleted();
void display();
struct Inventory
{
int record;
int quantity;
float cost;
}hardware[10];//, *hardware_ptr
//hardware_ptr[0] = hardware;
int main(int argc, char *argv[])
{
void (*functionptr[3])() = {update, deleted, display};
// Declare functionptr pointed to three function
int loop;
int choice;
printf("\n\t\t");
for (loop=0;loop<45;loop++)
{
printf("-");
}
printf("\n\n\t\t Welcome to Inventory System");
printf("\n\n\t\t");
for (loop=0;loop<45;loop++)
{
printf("-");
}
while(1)
{
printf("\n\n\n\t\t\t\tEnter a choice : ");
scanf("%d", &choice);
if (choice == 1)
{
choice = 0;
}
else if (choice == 2)
{
choice = 1;
}
else if ( choice == 3)
{
choice = 2;
}
else
{
perror("Invalid Choice");
}
// Advanced Techniques which make use of array of function pointer
// Call the function using functionptr
(*functionptr[choice])();
/* switch(choice)
{
case 1:
{
update();
}
case 2:
{
deleted();
}
case 3:
{
display();
}
default:
{
perror("Invalid Choice");
}
}*/
}
}
void update()
{
FILE *fp;
int loop;
fp = fopen("C:\\Test.txt", "a");
assert(fp!=NULL);
if (fp!=NULL)
{
printf("Enter the record number,quantity and cost for the tool name");
printf("\nRecord\t\tQuantity\t\tCost : \n");
for (loop=0;loop<10;loop++)
{
scanf("%d%d%f", &hardware[loop].record, &hardware[loop].quantity, &hardware[loop].cost);
printf("\nRecord\t\tQuantity\t\tCost : \n");
}
fprintf(fp, "\nRecord\t\tTool Name\t\tQuantity\t\tCost");
for (loop=0;loop<10;loop++)
{
fprintf(fp, "%d%d%f", hardware[loop].record, hardware[loop].quantity, hardware[loop].cost);
}
}
else
{
perror("File cannot be opened");
}
fclose(fp);
}
void deleted()
{
FILE *fp;
int record_number, loop;
fp = fopen("C:\\Test.txt", "a");
assert(fp!=NULL);
if (fp!=NULL)
{
printf("Enter a record number to delete : ");
scanf("%d", &record_number);
for (loop=0;loop<10;loop++)
{
if (record_number == hardware[loop].record)
{
hardware[loop].record = '0';;
hardware[loop].quantity = '0';
hardware[loop].cost = '0';
}
else
{
perror("Invalid Choice");
}
}
}
else
{
perror("File cannot be opened");
}
fclose(fp);
}
void display()
{
FILE *fp;
int loop;
fp = fopen("C:\\Test.txt", "r");
assert(fp!=NULL);
if (fp!=NULL)
{
printf("\nrecord\t\tTool Name\t\tQuantity\t\tCost\n\n");
for (loop=0;loop<10;loop++)
{
printf("%d %d %f\n", hardware[loop].record, hardware[loop].quantity, hardware[loop].cost);
}
}
else
{
perror("File canoot be opened");
}
fclose(fp);
}
I look forware for any help.
Thanks in advance for oyur help.
Your help is greatly appreciated by me an others.


