1 / 16

EXERCISE

EXERCISE. Arrays, structs and file processing. Question. You own a pet store. You want to keep an inventory of all the pets that you have. Pets available are cats, birds and fish. Your store can only keep up to 40 pets at a time.

varuna
Télécharger la présentation

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 • You own a pet store. You want to keep an inventory of all the pets that you have. Pets available are cats, birds and fish. Your store can only keep up to 40 pets at a time. • Use a struct for each pet. Each pet will have an ID, type and price. • Your program should read the input from keyboard, and then stores it into a file named “pets.dat”.

  3. After data input, your program can • display all cats. • display the total price of all pets

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

  5. Maximum pet is 40. So, use an Array of struct. structsPetmyPets [40] ;

  6. You need to read from users ( = keyboard). Because you have an array of struct (myPets[x]), you can put the inputs there. printf (“Enter ID : “ ); scanf (“%d” , &myPets[x].ID); printf (“Enter type: “); scanf (“%s”, &myPets[x].sType);

  7. Need to write into a file FILE *cPtr; cPtr = fopen (“pets.dat”, “w”); … … fprintf (cPtr, “\n %d %s %.2f” , myPets[x].ID, myPets[x].sType, myPets[x]. price) ; //this should be done in a loop

  8. Because it is from user input, you must ask the how many pets he/she wants to enter the data intnumPets; printf (“How many pets you want to enter ? Maximum is 40); scanf (“%d”, &numPets);

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

  10. Need to calculate the total price, so add inside the loop for (x = 0; x < numPets; x++) { . . . . totalPrice + = myPets [ x ].price; } //then, outside the loop printf (“total Price: %.2f”, totalPrice);

  11. To display the list of all cats, need another loop int a; //to hold value of compare string for ( x = 0; x < numItem; x++ ) { a = strcmp (myPets[x].sType, “cat”); if (a == 0) printf (“%d %s %.2f”, myPets[x].ID, myPets[x].sType, myPets[x].price) ; }

  12. Skeleton of a full program #include <stdio.h> #include <stdlib.h> structsPet { int ID; char sType[5]; float price; };

  13. int main() { structsPetmyPets [40] ; FILE *cPtr; cPtr = fopen (“pets.dat”, “w”); intnumPets; float totalPrice = 0; printf (“How many pets you want to enter ? Maximum is 40); scanf (“%d”, &numPets);

  14. for (x = 0; x < numPets; x++) { printf (“Enter ID : “ ); scanf(“%d” , &myPets[x].ID); printf(“Enter type: “); scanf(“%s”, &myPets[x].sType); printf (“Enter price: “); scanf (“%s”, &myPets[x].sType);

  15. //continue loop block fprintf(cPtr, “\n %d %s %.2f” , myPets[x].ID, myPets[x].sType, myPets[x]. price) ; totalPrice + = myPets [ x ].price; } //end of loop 1 printf (“total Price: %.2f”, totalPrice);

  16. int a; //to hold value of compare string for ( x = 0; x < numItem; x++ ) { a = strcpy (myPets[x].sType, “cat”); if (a == 0) printf (“%d %s %.2f”, myPets[x].ID, myPets[x].sType, myPets[x].price) ; } return 0; } //end of main

More Related