Using C programming, Pick either Albums, Movies, or Books as the type of invento
ID: 3772235 • Letter: U
Question
Using C programming,
Pick either Albums, Movies, or Books as the type of inventory to track. The assignment requires you to do the following with that inventory:
1. Allow the user to enter any number of inventory items.
2. Save the inventory data to a text file.
3. Read the inventory from a text file.
4. Search for a title and display inventory information about that title.
5. Store values in multiple arrays.
6. Demonstrate knowledge of one of the following:
7. Character arrays and string manipulation
8. Using arrays as pointers
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 25
#define MAX 100
typedef struct
{
char book[SIZE];
char company[SIZE];
int intStock; //how many of the book are instock
char lastShipDate[SIZE]; //last ship date of that book
double cost; //cost to make the book
double price; //price of the book
}
book_zone;
//prompts the user to get a selection
int Menu(void);
//display the options to the user
void DisplayOptions(void);
//display some entries for inventory
void HardCodeEntries(book_zone entry[],int *size);
//function to add a new entry to the inventory
void AddNewEntry(book_zone entry[], int *size);
//function to delete a selected entry from inventory
void Delete(book_zone entry[], int *size, int location);
//display the current inventory to screen
void Display(book_zone entry[], int size);
//save the current inventory to file
void SaveInventory(book_zone br[], int *size);
//clears out the entire inventory
void Clear(int *size);
//load the inventory from the file that is being saved
void LoadSalesRecords(book_zone br[], int *size);
//find location of entry that is going to be edited or deleted
int FindLocation(book_zone entry[], int size);
//display the options for editing an entry
int EditMenuOptions(void);
//function to edit selected entry
void Edit(book_zone entry[], int location);
int main()
{
int selection;
book_zone entry [150];
int size=0;
char trash;
int choice;
int location;
//display size of inventory before and after
printf(" SIZE before: %d", size);
HardCodeEntries(entry, &size);
printf(" SIZE after: %d ", size);
DisplayOptions();
selection= Menu();
while(selection != 8)
{
if (selection==1)
{
printf("ADD ENTRIES ");
printf(" SIZE BEFORE ADDING: %d ", size);
AddNewEntry(entry, &size);
printf(" SIZE AFTER ADDING: %d ", size);
}
else if(selection==2)
{
printf("DELETE ");
location = FindLocation(entry,size);
printf(" LOCATION: %d ", location);
Delete(entry, &size, location);
}
else if(selection==3)
{
printf("EDIT ");
location = FindLocation(entry, size);
printf(" LOCATION: %d ", location);
Edit(entry, location);
}
else if(selection==4)
{
printf("DISPLAY OF INVENTORY ");
Display(entry, size);
}
else if(selection==5)
{
printf("SAVING CURRENT INVENTORY TO FILE ");
SaveInventory(entry, &size);
//saves to file
}
else if(selection==6)
{
printf("CLEARING ENTIRE DATA ");
Clear(&size);
}
else if(selection==7)
{
printf("LOADING SALES RECORDS FROM FILE ");
LoadSalesRecords(entry, &size);
}
else
{
printf("COMMAND NOT RECOGNIZED ");
}
printf(" ");
DisplayOptions();
scanf("%c", &trash);
selection = Menu();
}
if (selection==8)
{
printf(" EXIT!!! ");
}
return 0;
}
//display the options to the user
void DisplayOptions(void)
{
printf("INVENTORY PROGRAM. ");
printf("1--ADD A NEW ENTRY ");
printf("2--DELETE AN ENTRY ");
printf("3--EDIT AN ENTRY ");
printf("4--DISPLAY THE INVENTORY ");
printf("5--SAVE CURRENT INVENTORY TO FILE ");
printf("6--CLEAR THE ENTIRE INVENTORY ");
printf("7--LOAD SALES RECORD FROM FILE ");
printf("8--QUIT ");
}
//prompts the user to get a selection
int Menu(void)
{
int selection;
printf(" ENTER SELECTION: ");
scanf("%d", &selection);
return selection;
}
//display some entries for inventory
void HardCodeEntries(book_zone entry[], int *size)
{
strcpy(entry[0].book,"LOVE");
strcpy(entry[0].lastShipDate,"04/10/12");
entry[0].cost=200.00;
strcpy(entry[0].company,"HBR Publication");
entry[0].price=300.0;
entry[0].intStock=150;
*size = *size + 1;
strcpy(entry[1].book,"FOOBAY");
strcpy(entry[1].lastShipDate,"04/10/12");
entry[1].cost=200.00;
strcpy(entry[1].company,"TATA Publication");
entry[1].price=450.00;
entry[1].intStock=100;
*size = *size + 1;
strcpy(entry[4].book,"Wii");
strcpy(entry[4].lastShipDate,"04/10/12");
entry[4].cost=100.00;
strcpy(entry[4].company,"Nintendo Publication");
entry[4].price=200.00;
entry[4].intStock=150;
*size = *size + 1;
}
//function to add a new enty to the inventory
void AddNewEntry(book_zone entry[], int *size)
{
printf(" ENTER book: ");
scanf("%s", entry[*size].book);
printf("ENTER SHIP DATE: ");
scanf("%s", entry[*size].lastShipDate);
printf("ENTER COST TO MAKE: ");
scanf("%lf", &entry[*size].cost);
printf("ENTER COMPANY: ");
scanf("%s", entry[*size].company);
printf("ENTER PRICE: ");
scanf("%lf", &entry[*size].price);
printf("ENTER NUMBER IN STOCK: ");
scanf("%d", &entry[*size].intStock);
*size = *size + 1;
}
//function to delete a selected entry from inventory
void Delete(book_zone entry[], int *size, int location)
{
entry[location] = entry[*size - 1];
*size = *size - 1;
}
//display the current inventory onto the screen
void Display(book_zone entry[], int size)
{
int i;
for(i=0; i<size; i++)
{
printf(" ");
printf("book: %s ", entry[i].book);
printf("LAST SHIP DATE: %s ", entry[i].lastShipDate);
printf("COST: %4.2f ",entry[i].cost);
printf("COMPANY: %s ", entry[i].company);
printf("PRICE: %4.2f ", entry[i].price);
printf("IN STOCK: %d ", entry[i].intStock);
}
}
//save the current inventory to a file
void SaveInventory(book_zone br[], int *size)
{
FILE *outp;
if((outp = fopen("output.txt","w"))==0)
{
printf(" CANNOT OPEN FILE ");
return;
}
//connect to the file
outp= fopen("output.txt","w");
fprintf(outp,"%d", *size);
fwrite(br, sizeof(book_zone),*size,outp);
//close file pointer
fclose(outp);
}
//clears out the entire inventory
void Clear(int *size)
{
*size = 0;
}
//load the inventory from the file that is being saved
void LoadSalesRecords(book_zone br[], int *size)
{
FILE * inp;
if((inp = fopen("output.txt","r"))==0)
{
printf("CANNOT OPEN FILE ");
return;
}
//connect to file
inp = fopen("output.txt","r");
fscanf(inp,"%d",size);
fread(br, sizeof(book_zone),*size,inp);
//close file pointer
fclose(inp);
}
//find location of entry that is going to be edited or deleted
int FindLocation(book_zone entry[], int size)
{
int i;
int j;
int length;
char userbook[SIZE];
//enter the book to delete
printf("ENTER book TO EDIT OR DELETE: ");
scanf("%s", userbook);
length = strlen(userbook);
for(i=0; i<length; i++)
{
userbook[i] = toupper(userbook[i]);
}
printf(" YOUR BRAND IN UPPER CASE IS: %s ", userbook);
for(j=0; j<size; j++)
{
if(strcmp(entry[j].book,userbook)==0)
{
return j;
}
}
return -1;
}
//function to edit a selected entry
void Edit(book_zone entry[], int location)
{
int choice;
choice = EditMenuOptions();
if(choice==1)
{
printf(" ENTER NEW book: ");
scanf("%s", &entry[location].book);
}
else if(choice==2)
{
printf(" ENTER SHIP DATE: ");
scanf("%s", &entry[location].lastShipDate);
}
else if(choice==3)
{
printf(" ENTER COST TO MAKE: ");
scanf("%lf", &entry[location].cost);
}
else if(choice==4)
{
printf(" ENTER COMPANY: ");
scanf("%s", &entry[location].company);
}
else if (choice==5)
{
printf(" ENTER PRICE: ");
scanf("%lf", &entry[location].price);
}
else if (choice==6)
{
printf(" ENTER HOW MANY IN STOCK: ");
scanf("%d", &entry[location].intStock);
}
else if (choice==7)
{
printf(" ENTER NEW book: ");
scanf("%s", &entry[location].book);
printf(" ENTER SHIP DATE: ");
scanf("%s", &entry[location].lastShipDate);
printf(" ENTER COST TO MAKE: ");
scanf("%lf", &entry[location].cost);
printf(" ENTER COMPANY: ");
scanf("%s", &entry[location].company);
printf(" ENTER PRICE: ");
scanf("%lf", &entry[location].price);
printf(" ENTER HOW MANY IN STOCK: ");
scanf("%d", &entry[location].intStock);
}
else
{
printf("Invalid Entry");
}
}
//display the options for edditing an entry
int EditMenuOptions(void)
{
int choice;
printf(" ENTER 1 TO EDIT book ");
printf("ENTER 2 TO EDIT DATE ");
printf("ENTER 3 TO EDIT COST ");
printf("ENTER 4 TO EDIT COMPANY ");
printf("ENTER 5 TO EDIT PRICE ");
printf("ENTER 6 TO EDIT NUMBERS IN STOCK ");
printf("ENTER 7 TO EDIT ALL DATA ");
printf(" ENTER SELECTION: ");
scanf("%d", &choice);
return choice;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.