This program is almost the same as the one last i posted but here the bubble sort algorithm is not working, i get a compiler error saying::
size of cdRecords is not known
and
need explicit cast to convert
I cant work out what the problem is because another program with exact same details but different structure works fine,, and yes i have tried converting that one to a CD database,, same problem though......
also the file output here is all jibrish,, this it shouldnt do..
//(Database Management System(DBMS), user interface and report generator for results.
//Also a diagnostic tool that allows access to the database metrics.
//This is to show the efficiency of the sort algorithms used.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#define MAX 10
//******************************************************************************************//
//Structure
struct CdRecords
{
char Artist[20];
char Album[20];
char Year[8];
char Label[20];
char Genre[20];
};
//******************************************************************************************//
// FUNCTIONS
void addRecord(struct CdRecords cdDB[]);
void listRecords(struct CdRecords cdDB[]);
void delRecord(struct CdRecords cdDB[]);
void init_list(struct CdRecords cdDB[]);
int menu_select(void);
void loadFile (struct CdRecords cdDB[]);
void saveFile(struct CdRecords cdDB[]);
void SearchTitle(struct CdRecords cdDB[]);
void findRecord(struct CdRecords cdDB[]);
void sortRecords(struct cdRecords cdDB[]);
void naive_sort (struct cdRecords cdDB[], int arraySize, int * count);
void swap (struct CdRecords * v1, struct cdRecords * v2);
const int datasize = 20;
//******************************************************************************************//
//MAIN FUNCTION
void main(void)
{
struct CdRecords cdDB[MAX];
char choice; //TO CAPTURE THE MENU SELECTION
init_list(cdDB); //CLEAR THE STRUCTURE
for (;;) { //LOOP AROUND MENU SELECTION UNTIL USER EXITS
choice =menu_select(); // CALL MENU_SELECT FUNCTION AND RETURN VALUE IN FIELD - CHOICE
switch(choice){
case 1:addRecord(cdDB);
break;
case 2:delRecord(cdDB);
break;
case 3:listRecords(cdDB);
break;
case 4:saveFile(cdDB);
break;
case 5:loadFile(cdDB);
break;
case 6:findRecord(cdDB);
break;
case 7:sortRecords(cdDB);
break;
case 8:exit(0);
break;
}//END SWITCH
}//END FOR
}
//******************************************************************************************//
//INITIALISE THE STRUCTURE
void init_list(struct CdRecords cdDB[])
{
int t; //TO CONTROL LOOP FOR CLEARING STRUCTURE
for (t=0;t<MAX; t++)
cdDB[t].Artist[0] = '\0'; //SET THE TITLE TO NULL
}
//******************************************************************************************//
//DISPLAY MENU OPTIONS
menu_select(void)
{char s [80];
int c;
printf("*** MAIN MENU ***\n");
printf("1.Enter\n"); //DISPLAY MENU OPTIONS TO USER
printf("2.Delete\n");
printf("3.List\n");
printf("4.Save File\n");
printf("5.Load File\n");
printf("6.Search\n");
printf("7.Sort\n");
printf("8.Exit\n");
do{
printf("Enter choice ");
gets(s);
c = atoi(s);
}while(c<0 ||c> 8); // EXIT APPLICATION WHEN USER SELECTS 7
return c;
}
//******************************************************************************************//
// ADD NEW ENTRIES TO THE FILE
void addRecord(struct CdRecords cdDB[])
{
int slot; // INTEGER TO LOCATE FREE SPACE IN STRUCTURE
char s[80]; // CALL THE FIND_FREE FUNCTION AND RETURN VALUE IN FIELD - SLOT
slot = find_free(cdDB);
if (slot==-1){ //IF SLOT = -1 THEN THE STRUCTURE IS FULL
printf("LIst Full");
return;
}
printf("Enter Artist: "); //PROMPT TO ADD TITLE
gets(cdDB[slot].Artist);
printf("Enter Album: "); //PROMPT TO ADD AUTHOR
gets(cdDB[slot].Album);
printf("Enter Label: "); //PROMPT TO ADD PUBLISHER
gets(cdDB[slot].Label);
printf("Enter Year: "); //PROMPT TO ADD YEAR
gets(cdDB[slot].Year);
printf("Enter Genre: "); //PROMPT TO ADD ISBN
gets(cdDB[slot].Genre);
}
//******************************************************************************************//
//SEARCH FOR A TITLE
void findRecord(struct CdRecords cdDB[])
{
system("CLS");
int i; //FIELD TO HOLD ARRAY COUNT FOR STRUCTURE
char SearchTitle[20]; //FIELD TO HOLD ENTERED TITLE TO BE SEARCHED FOR
int FoundRec; //FIELD TO INDICATE IF RECORD FOUND
printf("Enter Title :"); //PROMPT USER TO ENTER TITLE
scanf("%s", SearchTitle); //CAPTURE ENTERED TITLE
FoundRec = 0;
for(i = 0;i<datasize;i++){ //LOOP ROUND THE LIBRARY STRUCTURE CHECKING EACH TITLE
if((strcmp(SearchTitle,cdDB[i].Artist))==0){
printf("\n");
printf("%s\n",cdDB[i].Artist);
printf("%s\n",cdDB[i].Album);
printf("%s\n",cdDB[i].Label);
printf("%d\n",cdDB[i].Year);
printf("%s\n",cdDB[i].Genre);
FoundRec = 1;
}// END IF
}//END FOR
if (FoundRec == 0)
printf("Title Not Found \n"); //INFORM USER IF NO RECORD FOUND
printf("Hit Enter to Return to the Main Menu \n");
fflush(stdin);
getch();
}
//******************************************************************************************//
//FIND FREE SLOT TO ADD NEW ENTRY
int find_free(struct CdRecords cdDB[])
{
int t;
char test; //FIELD TO CHECK FOR A VALUE IN TITLE
for(t=0; t<MAX; t++){
test = cdDB[t].Artist[0]; //STORE TITLE IN TEST FIELD
if (test=='\0'){ //IF TEST FIELD IS NULL THERE IS AN EMPTY SLOT
return t; //RETURN THE SLOT NUMBER
}
}
return -1; //IF NO FREE SLOTS FOUND RETURN -1
}
//******************************************************************************************//
//DELETE THE SELECTED ITEM
void delRecord(struct CdRecords cdDB[])
{
int slot; //INTEGER TO HOLD SLOT NUMBER
char s[80]; //FIELD TO HOLD INPUTTED RECORD NUMBER TO DELETE
printf("Enter Record #: "); //PROMPT FOR RECORD NUMBER TO DELETE
gets(s);
slot = atoi(s); //CONVERT INPUT VALUE TO A SLOT NUMBER
if (slot>=0 && slot < MAX) //IF SLOT NUMBER IS SMALLER THAN MAX
cdDB[slot].Artist[0] = '\0'; //CLEAR SELECTED STRUCTURE ENTRY
}
//******************************************************************************************//
//DISPLAY THE LIST OF ENTRIES TO THE SCREEN
void listRecords(struct CdRecords cdDB[])
{
printf("\n\n LISTING LIBRARY FILE....\n");
int t; //INTEGER TO CONTROL FOR LOOP
for (t=0; t<MAX; ++t) {
if (cdDB[t].Artist[0]) { //IF THERE IS DATA IN THE SELECTED TITLE
printf("%s\n",cdDB[t].Artist); //PRINT ALL DETAILS TO SCREEN
printf("%s\n",cdDB[t].Album);
printf("%s\n",cdDB[t].Label);
printf("%d\n",cdDB[t].Year);
printf("%s\n",cdDB[t].Genre);
}
}
printf("\n\n");
}
//******************************************************************************************//
//SAVE THE cdDB STRUCTURE DETAILS TO THE music FILE
void saveFile(struct CdRecords cdDB[])
{
printf("\n\n SAVING FILE.......\n");
FILE *fp; //DEFINE THE FILE POINTER
int i;
if ((fp=fopen("music.txt","wb"))==NULL){ //OPEN THE FILE - CAPTURE RESULT
printf("Cannot Open File \n"); //AND DISPLAY MESSAGE TO USER IF
return; //UNABLE TO LOCATE FILE
}
for (i=0; i<MAX; i++) //LOOP ROUND CHECKING FOR A VALUE IN TITLE
if (*cdDB[i].Artist) //AND SAVE ALL STRUCTURE DETAILS TO FILE
//if (fwrite(&cdDB[i], //CHECK THAT WE ARE NOT EXCEEDING SIZE OF
//sizeof(struct cdRecords),1,fp)!=1) //STRUCTURE
//printf("File Write Error\n"); //IF YES DISPLAY MESSAGE TO USER
fclose(fp);
}
//******************************************************************************************//
//LOAD THE DETAILS OF THE LIBRARY FILE TO THE LIB STRUCTURE
void loadFile (struct CdRecords cdDB[])
{
printf("\n\n LOADING FILE.......\n");
FILE *fp; //DEFINE THE FILE POINTER
int i;
if ((fp=fopen("music.txt","rb"))==NULL){ //OPEN THE FILE - IF NOT FOUND
printf("Cannot Open File \n"); //DISPLAY MESSAGE TO USER
return;
}
init_list(cdDB); //CLEAR STRUCTURE
for (i=0; i<MAX; i++) //LOOP ROUND AND
if (fread(&cdDB[i], //COPY FILE RECORDS TO THE STRUCTURE
sizeof(struct CdRecords),1,fp)!=1){
if (feof(fp)) break; //CHECK FOR ERROR IN READING FILE
printf("File Read Error\n");
//******************************************************************************************//
// SORT FILE BY TITLE USING BUBBLESORT METHOD
void sortRecords(struct cdRecords cdDB[])
{
system("CLS");
int count = 0;
naive_sort (cdDB, datasize, & count);
//if(ch==3){
//q_sort (cdDB, count);
// }
printf("\nCritical count is : %d\n",count);
printf("Array has been sorted");
printf("Press Enter To Continue");
fflush(stdin);
getch();
}
// BUBBLESORT METHOD
void naive_sort (struct cdRecords array [], int arraySize, int * count)
{
for (int pass = 0; pass <= arraySize - 2; pass++)
{ for (int counter = 0; counter <= arraySize - 2-pass; counter++)
{
*count = *count + 1; // count critical operations
if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
swap (&array[counter], &array[counter+1]);
}
}
}
// SWAP A PAIR OF VALUES
void swap (struct CdRecords * v1, struct cdRecords * v2)
{
struct CdRecords temp;
temp = *v1;
*v1 = *v2;
*v2 = temp;
}