Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

when i compile the following code, command prompt stops working after getting in

ID: 3543774 • Letter: W

Question

when i compile the following code, command prompt stops working after getting inputs. im stuck. i think there should be a problem with memory allocation. please help


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


struct album {

     char *albumName;

     char *singerName;

     int year;

     struct album* nextAlbum;

     struct song* nextSong;

};


struct song {

     char *songName;

     int length;

     struct song* nextSong;

};


typedef struct album album;

typedef struct album* albumPtr;

typedef struct album** albumPtrPtr;


typedef struct song snode;

typedef struct song* snodePtr;

typedef struct song** snodePtrPtr;


int addAlbum(albumPtrPtr,char*,char*,int);

void main() {

int choice; albumPtr hdr;

int year; char* x; char* y;

   printf("Enter choice: ");

   scanf("%d", &choice);

   switch(choice) {

   case 1:

      printf("Enter albumname singername and year: ");

       scanf("%s %s %d",x,y,&year);

       addAlbum(&hdr,x,y,year);

       break;

       }

}




int addAlbum(albumPtrPtr header, char *albumNamee, char *singerNamee, int year) {

    albumPtr newAlbum, prevAlbum, tempAlbum;


    newAlbum = malloc(sizeof(album));


    newAlbum -> albumName = malloc(strlen(albumNamee)+1);

    newAlbum -> singerName = malloc(strlen(singerNamee)+1);


    strcpy(newAlbum -> albumName, albumNamee);

    strcpy(newAlbum -> singerName, singerNamee);

    newAlbum -> year = year;


    newAlbum -> nextAlbum = NULL;

    if(*header == NULL)

        *header = newAlbum;


    else {

        tempAlbum = *header;

        while(tempAlbum != NULL && tempAlbum -> year < newAlbum -> year) {

            prevAlbum = tempAlbum;

            tempAlbum -> nextAlbum;

        }


        if(tempAlbum != NULL && tempAlbum -> albumName == newAlbum -> albumName) {

            printf("Album already exists. ");

           return 0;

        }


        if(tempAlbum != NULL)

            newAlbum -> nextAlbum = tempAlbum;


        if(tempAlbum == *header)

            *header = newAlbum;


        else

            prevAlbum -> nextAlbum = newAlbum;

    }


   return 1;

}

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


struct album {

char *albumName;

char *singerName;

int year;

struct album* nextAlbum;

struct song* nextSong;

};


struct song {

char *songName;

int length;

struct song* nextSong;

};


typedef struct album album;

typedef struct album* albumPtr;

typedef struct album** albumPtrPtr;


typedef struct song snode;

typedef struct song* snodePtr;

typedef struct song** snodePtrPtr;


int addAlbum(albumPtrPtr,char*,char*,int);

void main() {

int choice; albumPtr hdr=NULL;

int year; char* x; char* y;

x = malloc(20*sizeof(char));

y = malloc(20*sizeof(char));

printf("Enter choice: ");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter albumname singername and year: ");

scanf("%s %s %d",x,y,&year);

addAlbum(&hdr,x,y,year);

break;

default:break;

}

}




int addAlbum(albumPtrPtr header, char *albumNamee, char *singerNamee, int year) {

albumPtr newAlbum, prevAlbum, tempAlbum;


newAlbum = malloc(sizeof(album));


newAlbum -> albumName = albumNamee;

newAlbum -> singerName = singerNamee;


newAlbum -> year = year;


newAlbum -> nextAlbum = NULL;

if(*header == NULL)

*header = newAlbum;


else {

tempAlbum = *header;

while(tempAlbum != NULL && tempAlbum -> year < newAlbum -> year) {

prevAlbum = tempAlbum;

tempAlbum = tempAlbum -> nextAlbum;

}


if(tempAlbum != NULL && strcmp(tempAlbum -> albumName, newAlbum -> albumName)==0) {

printf("Album already exists. ");

return 0;

}


else if(tempAlbum != NULL)

newAlbum -> nextAlbum = tempAlbum;


else if(tempAlbum == *header)

*header = newAlbum;


else

prevAlbum -> nextAlbum = newAlbum;

}


return 1;

}