Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 struct sPet { int ID; char sType[5]; float price; };

5 2)Maximum pet is 40. So, use an Array of struct. struct sPet myPets [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 int numPets; 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 struct sPet { int ID; char sType[5]; float price; };

13 int main() { struct sPet myPets [40] ; FILE *cPtr; cPtr = fopen (“pets.dat”, “w”); int numPets; 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


Download ppt "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."

Similar presentations


Ads by Google