180 likes | 326 Views
EXERCISE. Arrays, structs and file processing. Question. An online store sells various types of children’s clothing. You are to create a program that stores the inventory. Use a struct for each clothing item. Each item will have an ID, type, cost and price.
E N D
EXERCISE Arrays, structs and file processing
Question • An online store sells various types of children’s clothing. You are to create a program that stores the inventory. • Use a struct for each clothing item. Each item will have an ID, type, cost and price. • Your program should read the input from keyboard, and then stores it into a file named “stock.dat”. Make sure that the new data is appended to the existing file (Don’t overwrite)
Limit your items to 150 only each time. • Your program can also • display a list of particular items. Example, if a user asks for ‘dress’, your program will display a list of all dresses available in the stock • display the total cost of all clothings.
Steps to solve 1) Define the struct structsItem { int ID; char sType[15]; float cost; float price; };
Limit of item is 150. So, use an Array of struct. structsItemsShop [150] ;
You need to read from users ( = keyboard). Because you have an array of struct(sShop[x]), you can put the inputs there. printf (“Enter ID : “ ); scanf (“%d” , &sShop[x].ID); printf (“Enter type: “); scanf (“%s”, &sShop[x].sType);
Need to write into a file FILE *cPtr; cPtr = fopen(“stock.dat”, “a”); … … fprintf (cPtr, “\n %d %s %.2f %.2f” , sShop[x].ID, sShop[x].sType, sShop[x]. cost, sShop[x]. price) ; //this should be done in a loop fclose(cPtr);
Because it is from user input, you must ask the how many items he/she wants to enter the data intnumItems; printf (“How many items you want to enter ? Maximum is 40); scanf (“%d”, &numItems);
An array of items, so must have ‘for’ loops for (x = 0; x < numitems; x++) { . . . . . . . . }
Need to calculate the total cost, so add inside the loop for (x = 0; x < numitems; x++) { . . . . totalCost+ = sShop[ x ].cost; } //then, outside the loop printf (“total Cost: %.2f”, totalCost);
To display the list of a type of clothing, need to ask user the item requested printf (“Enter a type of item to search :”); scanf (“%s”, &searchKey);
need to read from the file to search for items requested by user cPtr = fopen(“stocks.dat”, “r”); while (!feof(cPtr)) { fscanf (cPtr, “%d %s %f”, &ID, &type, &price); if ((strcmp(type, searchKey)==0)) printf (“\n %d %s %f”, ID, type, price); } fclose (cPtr);
Skeleton of a full program #include <stdio.h> #include <stdlib.h> structsItem { int ID; char sType[15]; float cost; float price; };
int main() { structsItemsShop [150] ; FILE *cPtr; cPtr = fopen(“stocks.dat”, “a”); intnumitems; float totalCost= 0; printf (“How many items you want to enter ? Maximum is 40); scanf (“%d”, &numitems);
for (x = 0; x < numitems; x++) { printf (“Enter ID : “ ); scanf(“%d” , &sShop[x].ID); printf(“Enter type: “); scanf(“%s”, &sShop[x].sType); printf (“Enter cost: “); scanf (“%s”, &sShop[x].cost); printf(“Enter price: “); scanf (“%s”, &sShop[x].price);
//continue loop block fprintf(cPtr, “\n %d %s %.2f %.2f” , sShop[x].ID, sShop[x].sType, sShop[x]. cost, sShop[x].price); totalCost+ = sShop[ x ].cost; } //end of loop 1 fclose (cPtr); printf (“total Cost: %.2f”, totalCost);
printf (“Enter a type of item to search :”); scanf (“%s”, &searchKey); cPtr = fopen(“stocks.dat”, “r”); //open again but for read-only while (!feof(cPtr)) { fscanf (cPtr, “%d %s %f”, &ID, &type, &price); if ((strcmp(type, searchKey)==0)) printf (“\n %d %s %f”, ID, type, price); } fclose (cPtr); return 0; } //end of main
End notes! • After you have obtained the skeleton program, expect to find some errors, e.g: • Undeclared variables • Misspellings • Wrongly located code segments • However, that is fine. At least you already have a framework where you can work on to refine and rectify.