Need help on fixing code in C Question: C program Many of us have large digital
ID: 3889699 • Letter: N
Question
Need help on fixing code in C
Question: C program Many of us have large digital music collections that are not always very well organized...
C program
Many of us have large digital music collections that are not always very well organized. It would be nice to have a program that would manipulate our music collection based on attributes such as artist, album title, song title, genre, song length, number times played, and rating. For this assignment you will write a basic digital music manager (DMM).
Your DMM program must have a text-based interface which allows the user to select from a main menu of options including: (1) load, (2) store, (3) display, (4) insert, (5) delete, (6) edit, (7) sort, (8) rate, (9) play, (10) shuffle, and (11) exit. For Part I of the assignment, you will only need to complete the main menu, (1) load, (2) store, (3) display, (6) edit, (8) rate, (9) play, and (11) exit features. The other features will be completed in the next part of the assignment.
What must the main menu contain?
The main menu must display the following commands:
(1) load
(2) store
(3) display
(4) insert
(5) delete
(6) edit
(7) sort
(8) rate
(9) play
(10) shuffle
(11) exit
After a command is selected and completed, your program must display the main menu again. This procedure will continue until the “exit” command is selected.
What must “load” do?
The “load” command must read all records from a file called musicPlayList.csv (you may find a sample file here) into a dynamic doubly linked list. The doubly linked list is considered the main playlist. As each record is read from the file, it must be inserted at the front of the list. Each record consists of the following attributes:
Artist – a string
Album title – a string
Song title – a string
Genre – a string
Song length - a struct Duration type consisting of seconds and minutes, both integers
Number times played – an integer
Rating – an integer (1 – 5)
Each attribute, in a single record, will be separated by a comma in the .csv (comma separated values) file. This means that you will need to design an algorithm to extract the required attributes for each record. Each field in each record will have a value. You do not need to check for null or empty values.
You must define a struct called Record to represent the above attributes. Also, do not forget that the Song Length must be represented by another struct called Duration. Duration is defined as follows:
Minutes – an integer
Seconds – an integer
Finally, each struct Node in the doubly linked list must be defined as follows:
Data – a Record
Pointer to the next node
Pointer to the previous node
What must “store” do?
The “store” command writes the current records, in the dynamic doubly linked list, to the musicPlayList.csv file. The store will completely overwrite the previous contents in the file.
What must “display” do?
The “display” command prints records to the screen. This command must support two methods, one of which is selected by the user:
Print all records.
Print all records that match an artist.
What must “edit” do?
The “edit” command must allow the user to find a record in the list by artist. If there are multiple records with the same artist, then your program must prompt the user which one to edit. The user may modify all of the attributes in the record.
What must “rate” do?
The “rate” command must allow the user to assign a value of 1 – 5 to a song; 1 is the lowest rating and 5 is the highest rating. The rating will replace the previous rating.
What must “play” do?
The “play” command must allow the user to select a song, and must start “playing” each song in order from the current song. “Playing” the song for this assignment means displaying the contents of the record that represents the song for a short period of time, clearing the screen and showing the next record in the list, etc. This continues until all songs have been played.
What must “exit” do?
The “exit” command saves the most recent list to the musicPlayList.csv file. This command will completelyoverwrite the previous contents in the file.
Here is my code : https://drive.google.com/open?id=0B28HOM-IIfXea3ZOZ2xhM2lkdjA
here is the data: https://drive.google.com/open?id=0B28HOM-IIfXeTGlhaWF0NndjclU
My code got some problem with the loading and display please help me fix it thank you
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define strlength 25;
struct node {
struct node *prev;
char artist[strlength];
char albumtitle[strlength];
char songtitle[strlength];
char genre[strlength];
int songlen;
int num_ts_played;
int rating;
struct node *next;
} *h, *temp, *temp1, *temp2, *temp4, *temp3;
void insert();
void delete();
void sort();
void edit();
void store(int);
void load();
void display();
void rate();
int count = 0;
int count1=0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
printf(" 1 - load");
printf(" 2 - store");
printf(" 3 - display");
printf(" 4 - insert");
printf(" 5 - delete");
printf(" 6 - sort");
printf(" 7 - edit");
printf(" 8 - rate");
printf(" 9 - exit");
while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
load();
break;
case 2:
store();
break;
case 3:
display();
break;
case 4:
insert();
sort();
break;
case 5:
delete();
break;
case 6:
sort();
break;
case 7:
edit();
break;
case 8:
rate();
break;
case 9:
exit(0);
break;
default:
printf(" Wrong choice menu");
}
}
}
/* TO create an empty node */
void create()
{
int data;
temp = (struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("Artist Name: ");
scanf("%s", temp->artist);
printf("Album title: ");
scanf("%s", temp->albumtitle);
printf("Song title: ");
scanf("%s", temp->songtitle);
printf("Generic name: ");
scanf("%s", temp->genre);
printf("Length of the song: ");
scanf("%d", temp->songlen);
printf("Number of times played: ");
scanf("%d", temp->num_ts_played);
printf("Rating: ");
scanf("%d", temp->rating);
count++;
}
/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To delete an element */
void delete()
{
int i = 1;
char song[strlength];
printf(" Enter title of the song : ");
scanf("%s", song);
temp2 = h;
if (strcmp(temp2->songtitle, song) == 0)
{
printf(" Error : Position out of range to delete");
return;
}
if (h == NULL)
{
printf(" Error : Empty list no elements to delete");
return;
}
else
{
while (temp2->songtitle, song)
== 0)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */
if (i == 1)
h = temp2->next;
printf(" Node deleted");
free(temp2);
}
count--;
}
/* Traverse from beginning */
void display() {
temp2 = h;
if (temp2 == NULL)
{
printf("List empty to display ");
return;
}
printf(" Linked list elements from begining : ");
while (temp2->next != NULL)
{
printf(" %s ", temp2->artist);
printf(" %s ", temp2->albumtitle);
printf(" %s ", temp2->songtitle);
printf(" %s ", temp2->genre);
printf(" %d ", temp2->songlen);
printf(" %d ", temp2->num_ts_played);
printf(" %d ", temp2->rating);
temp2 = temp2->next;
}
printf(" %s ", temp2->songtitle);
}
/* To search for an element in the list */
void search() {
int count = 0;
char artist[strlength];
temp2 = h;
if (temp2 == NULL)
{
printf(" Error : List empty to search for data");
return;
}
printf(" Enter value to search : ");
scanf("%s", artist);
while (temp2 != NULL)
{
if (strcmp(temp2->artist, artist) == 0)
{
printf(" Data found in %d position", count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf(" Error : %d not found in list", data);
}
/* To update a node value in the list */
void edit()
{
char album[strlength];
char album1[strlength];
printf(" Enter album title to be updated : ");
scanf("%d", &album);
printf(" Enter new data : ");
scanf("%d", &album1);
temp2 = h;
if (temp2 == NULL)
{
printf(" Error : List empty no node to update");
return;
}
while (temp2 != NULL)
{
if (strcmp(temp2->albumtitle, album) == 0)
{
temp2->albumtitle = album;
display();
return;
}
else
temp2 = temp2->next;
}
printf(" Error : %d not found in list to update", data);
}
/* To sort the linked list */
void sort()
{
int i, j, x;
temp2 = h;
temp4 = h;
temp3 = h;
if (temp2 == NULL)
{
printf(" List empty to sort");
return;
}
for (temp2 = h; temp2 != NULL; temp2 = temp2->next)
{
for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
{
if (strcmp(temp2->genre, temp4->genre) > 0)
{
temp3 = temp2;
temp2 = temp4;
temp4 = temp3;
}
}
}
display();
}
void load()
{
FILE *fp;
char ch;
fp = fopen("test.txt", "r");
while ((ch = getc(file)) != EOF)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
fscanf("%s", temp->artist);
fscanf("%s", temp->albumtitle);
fscanf("%s", temp->songtitle);
fscanf("%s", temp->genre);
fscanf("%d", temp->songlen);
fscanf("%d", temp->num_ts_played);
fscanf("%d", temp->rating);
temp->next = h;
h->prev = temp;
h = temp;
count1++;
}
fclose(fp);
}
void store()
{
FILE *fp;
temp = (struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
char ch;
fp = fopen("test.txt", "w");
if (fp)
{
for (i = 0; i <= count1; ++i)
{
fprintf("%s ", temp->artist);
fprintf("%s ", temp->albumtitle);
fprintf("%s ", temp->songtitle);
fprintf("%s ", temp->genre);
fprintf("%d ", temp->songlen);
fprintf("%d ", temp->num_ts_played);
fprintf("%d ", temp->rating);
fprintf(" ");
}
}
fclose(fp);
}
void rate()
{
temp2 = h;
char song[strlength];
int r;
printf("rate the songs: ");
printf("enter the song to rate: ");
scanf("%s", &song);
if (temp2 == NULL)
{
printf("List empty to display ");
return;
}
while (temp2->next != NULL)
{
if(strcmp(temp2->songtitle,song)==0)
{
printf("enter the rate(1-5): ");
scanf("%d", r);
temp2->rating=r;
}
else
{
temp2=temp2->next;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.