main() { cd_t cds[NO_CDS]; int count = 0; /* how many CDs are being tracked */ int i; /* loop counter */ puts("Welcome to the CD database."); printf("You can store a maximum of %d CDs.\n", sizeof cds / sizeof cds); /* * Loop until they no longer wish to enter any more CDs */ for (;;) /* forever loops are convenient for this sort of thing */ { /* * Ask them if they want to enter another CD */ if (!yesno("\nHave you any more CDs to enter")) break; printf("\nPlease enter the details of CD %d...\n\n", count+1); /* * Read all the CD details */ read_cd(&cds[count]); /* * Check if we have filled up the array */ if (++count == NO_CDS) /* note the increment happens before the test */ { enter("You have reached the limits of this program\n" "Press ENTER to continue: "); break; } } /* * Output the CD details */ for (i = 0; i < count; i++) { printf("\nThe details of CD %d are:\n", i+1); print_cd(&cds[i]); if (i < count - 1) /* only do this if there are more CDs to see */ enter("\nPress ENTER to see the next set of details: "); } /* * Exit the program */ enter("\nPress ENTER to exit the program: "); }
int main() { int count = 0; // How many CDs are being tracked int i; // Loop counter puts("Welcome to the CD database"); printf("You can store a maximum of %d CDs\n\n", sizeof cds / sizeof cds[0]); // Loop until user no longer wish to enter any more CDs for (;;) { // Ask the user if they want to enter another CD if (!yesno("\nDo you have any more CDs to enter")) break; printf("Please enter the details of CD %d...\n", count+1); // Read all the CD details read_cd(&cds[count]); // Check if array has been filled up if (++count == MAX_CDS) // Note increment happens before the test { enter("You have reached the limits of this program\n" "Press ENTER to exit the program"); break; } } // Output the details of the CD for (i = 0; i < count; i++) { printf("\nThe details of CD %d are:\n", i+1); print_cd(&cds[i]); // Only do this if there are more CDs to see if (i < count - 1) { // User-friendly way to progress to the next CD enter("\nPress ENTER to see the next set of details: "); } } // User-friendly way to exit the program enter("\nPress ENTER to exit the program "); }