1 / 18

EXERCISE

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.

libba
Download Presentation

EXERCISE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EXERCISE Arrays, structs and file processing

  2. 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)

  3. 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.

  4. Steps to solve 1) Define the struct structsItem { int ID; char sType[15]; float cost; float price; };

  5. Limit of item is 150. So, use an Array of struct. structsItemsShop [150] ;

  6. 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);

  7. 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);

  8. 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);

  9. An array of items, so must have ‘for’ loops for (x = 0; x < numitems; x++) { . . . . . . . . }

  10. 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);

  11. 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);

  12. 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);

  13. Skeleton of a full program #include <stdio.h> #include <stdlib.h> structsItem { int ID; char sType[15]; float cost; float price; };

  14. 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);

  15. 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);

  16. //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);

  17. 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

  18. 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.

More Related