160 likes | 278 Views
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.
E N D
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. • 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”.
After data input, your program can • display all cats. • display the total price of all pets
Steps to solve 1) Define the struct structsPet { int ID; char sType[5]; float price; };
Maximum pet is 40. So, use an Array of struct. structsPetmyPets [40] ;
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);
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
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);
An array of pets, so must have ‘for’ loops for (x = 0; x < numPets; x++) { . . . . . . . . }
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);
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) ; }
Skeleton of a full program #include <stdio.h> #include <stdlib.h> structsPet { int ID; char sType[5]; float price; };
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);
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);
//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);
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