ok.. I retried code (couldnt use getchar() ) and I have one problem. When i try play a song it crashes. Anyone able to help?
Code:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
struct node//creates a struct called node which holds artist name,song title,a pointer to next node and a counter
{
char artist[80];
char title[80];
struct node*link;
int counter;
};
typedef struct node N;//defines struct node as N
N*createcd(N*cd,char name[],char song[],int counter);//prototype for create cd function
N*search(N*head,char key[]);//prototype for search function
void playsong(N*node);//prototype for playsong function
N*createlist(N*head);//prototype for create list function
void displaylist(N*head);//prototype for display list function
void main()
{
N*head=NULL;//sets the value of head of the list to 0
int choice=0;//control variable for loop
char songname[80];//name of song wanted in play song function
N*position;//gives position of found song
do{
printf("\t\tWelcome to CD Player\n\n");//prints out menu
printf("\t1.Create a CD\n");
printf("\t2.Play a CD\n");
printf("\t3.Display List\n");
printf("\t0.Press to exit\n\n");
printf("Please enter your choice\n");
scanf("%d",&choice);
if(choice==1)//this is the create cd section, it creates the cd and then displays the link list
{
head=createlist(head);
displaylist(head);
}
else if(choice==2)//this is the play song section, it prompts the user for the name of the song they want to play, it finds it in the list and then returns it to the play song function
{
printf("Please enter the name of the song you want to play\n");
scanf("%s",songname);
position=search(head,songname);
playsong(position);
}
else if(choice==3)
{
displaylist(head);
}
else if(choice!=0)
{
printf("Invalid Choice, Please try Again\n");
}
}while(!choice==0);
}
N*createcd(N*cd,char name[],char song[],int count)
{
cd=malloc(sizeof(N));
cd->counter=count;//copies no of plays from the input to the node
strcpy(cd->artist,name);//copies artist from input to node
strcpy(cd->title,song);//copies song title from input top node
cd->link=NULL;//makes the pointer point to the end of the list
return cd;//returns the edited node
}
N*search(N*head,char key[])//finds the song wanted in the link list
{
N * current;
current=head;
//if (current->word < key) move through links until the next link
//matches or current_word > key
while(current!=NULL)
{
//compare the keyword with the link list
if (strcmp(key,current->title)==0)
{
break;
}
current = current -> link;
}
if (current==head) //empty list
{
return NULL;
}
else
{
// returns the position of the node
return current;
}
}
void playsong(N*node)//plays the song
{
node->counter++;//increments counter
system("cls");
printf("'%s' by %s\n",node->title,node->artist);//displays name of song
printf("Played %d times\n",node->counter);//displays number of times song played
}
N*createlist(N*head)//this links all the created nodes together
{
N*last = head;//initially sets end of the list to head
char artist[80];
char songtitle[80];
if (head !=NULL)//if head isnt empty
{
while(last->link!=NULL)
{
last=last->link;
}
}
printf("Please enter artist name as one word\n");//prompts user to enter artist name
scanf("%s",artist);
printf("Please enter song title as one word\n");//prompts user to enter song name
scanf("%s",songtitle);
if(head==NULL)//if link list is empty
{
head=createcd(head,artist,songtitle,0);//create a cd
last=head;
}
else//if link list isnt empty
{
last->link=createcd(last->link,artist,songtitle,0);
last=last->link;//sets the end of the list
}
return head;
}
void displaylist(N*head)
{
N*current;
current=head;
if (current==NULL)//if current is 0 the link list is empty
{
printf("Empty list\n\n");
}
else//otherwise print out the names of all the songs until current =0
{
while(current!=NULL)
{
printf("%s by %s.Played %d times\n",current->title,current->artist,current->counter);
current=current->link;
}
}
}