Code:
/*
*Database for storing music cd.
*/
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<cs50.h>
#define MAX 200 //max characters a user can enter
/*prototypes*/
void write_record(void);
int file_exists(void);
void file_new(void);
void file_open(void);
void delete_rec_no(void);
void display(void);
/*structure for music cd*/
typedef struct cd
{
char artist[MAX];
char title[MAX];
int tracks;
int album;
float price;
struct cd *next;
}cd;
/* Global Variables: */
cd *head = NULL; // a pointer that points to start of the cd node.
cd *curr; // pointer that point to current cd node
int changes = 0; // boolean: if changes made or not
char fname[MAX]; // stores a name of a file.
int count; //stores numbers of cds
/*function that shows up on exit*/
void quit(void)
{
printf("\nThank you for using database. Press enter to exit\n");
while(getchar() != '\n');
}
/*Function that asks a question and demands y\n answer*/
int yesno(char prompt[])
{
char answer;
/*Loop will only break if a user enters 'y' or 'n'*/
for(;;)
{
printf("%s (y/n) ",prompt);
scanf (" %c",&answer);
answer = toupper(answer);
if(answer == 'Y' || answer == 'N')
break;
}
if(answer == 'Y') //if answer is 'y' then return 1 else return 0
return 1;
else
return 0;
}
/*Function to see if a file exists*/
int file_exists(void)
{
FILE *fptr = fopen(fname,"rb"); //open a file for reading.
if(fptr == NULL) //if file does not exists return 0.
return 0;
else //if file exists:
{
fclose(fptr); //close file
return 1; //return 1.
}
}
/*Function to ask the user for cd details*/
void input(void)
{
printf("Title: ");
scanf (" %s",curr->title);
printf("Artist: ");
scanf (" %s",curr->artist);
printf("Tracks: ");
scanf (" %d",&curr->tracks);
curr->album = yesno ("Artist?");
printf("Price: ");
scanf (" %f",&curr->price);
}
/*Function to reset everything back*/
void reset_all(void)
{
changes = 0;
fname[0] = '\0';
head = NULL;
curr = NULL;
count = 0;
}
/*Function to show unsaved records*/
void display(void)
{
cd *current;
current = head;
while(current != NULL)
{
printf("Title: %s\n",current->title);
printf("Artist: %s\n",current->artist);
printf("Tracks: %d\n",current->tracks);
printf("%s\n", current->album ? "album" : "single");
printf("Price: %f\n",current->price);
current = current->next;
}
return;
}
int main(void)
{
int choice; //variable that will store choice which the user enters
atexit(quit); //calls 'quit' function b4 program exits
/*Loop that will only break on exit*/
for(;;)
{
printf("\nCurrent file open: %s\n",fname); // print current file opened
/*print menu*/
puts("\nMenu: \n");
puts(" 1. File new");
puts(" 2. File open");
puts(" 3. Write a record");
puts(" 11. Show records");
puts(" 4. Delete a record by number");
puts(" 5. Delete a record by title");
puts(" 6. Save records");
puts(" 7. Save records with a new file name");
puts(" 8. Edit a record");
puts(" 9. Delete a file");
puts(" 10. Exit");
scanf(" %d",&choice); //ask user for choice
switch(choice)
{
case 1:
file_new();
break;
case 2:
file_open();
break;
case 3:
write_record();
break;
case 4:
delete_rec_no();
break;
case 10:
exit(0);
break;
case 11:
display();
break;
}
}
}
/*function that resets everything*/
void file_new(void)
{
/*check if there are changes and then see if a the user wants to discard the records*/
if(changes && yesno("Are you sure you want to discard the records?"))
{
return;
}
reset_all();
return;
}
void file_open(void)
{
/*check if there are changes and then see if a the user wants to discard the records*/
if(changes && yesno("Are you sure you want to discard the records?"))
{
return;
}
reset_all();
/*ask the user the file name they would like to open*/
printf("Enter the file name you would like to open: ");
scanf (" %s",fname);
/*check if the file exists*/
if(!file_exists())
{
printf("File does not exists!\n");
fname[0] = '\0';
return;
}
}
/*function to write a record in dynamic memory*/
void write_record(void)
{
if(head == NULL) // check to see if head has not yet been assigned:
{
head = malloc(sizeof(cd)); // allocate memory for head
curr = head; // set curr to head
input(); //call input function
++count; //count increases by 1 everytime user writes a record
}
else //if head has already been assigned:
{
curr->next = malloc(sizeof(cd)); //malloc next node
curr = curr->next; //set curr to next node
input(); //call 'input' function
++count; //count increases by 1 everytime user writes a record
}
return;
}
/*Function to delete a record*/
void delete_rec_no(void)
{
int number; //stores the number which user wants to be deleted
cd *prev;
cd *current;
int i;
if(count == 0)
{
printf("There are no cds input!\n");
return;
}
do
{
printf("Enter the number of cd which you would like to remove: ");
scanf(" %d",&number);
if(number > count || number < 0)
printf("The number you entered is invalid. There are currently %d cds.\n",count);
else
break;
}while(number > count || number < 0);
current = head;
prev = head;
for(i = 2; i <= number; i++)
{
if(number == 1)
{
head = current->next;
free(current);
}
if(i == number)
{
prev->next = current->next;
free(current);
}
prev = current;
current = current->next;
i++;
}
// display();
return;
}
whats the problem in display function and delete_rec_no???????