0

I am getting an unexpected break point when I am trying to print out my doubly linked list. The unexpected break point is popping out in the printSongInfo function in the dsA1LL.cpp on the line

while (ptr != NULL) {
    printf("%-35s %-35s %-35d\n", ptr->title, ptr->artist, ptr->rating);
    ptr = ptr->next;
}

//New just added I completed some changes to the piece of code and now it not printing out the list. It is only printing out the first Node.

This will only print one line then the break point appears. I am wondering if it is the printSongInfo function or if it is the doubly linked list function getSongInfo causing the issue. Source code dsA1.cpp file

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include"dsA1.h"
#pragma warning(disable : 4996)
#pragma warning(disable : 6387)


int main() {


    char title[kStringLength];
    char artist[kStringLength];
    int rating = 0; 
    SongNode* head = NULL; 
    SongNode* tail = NULL; 



    printf("Enter Title, Artist and Rating\n");
    printf("Enter'.' to get out of the loop and print list\n");
    
    while (kInfiniteLoop) {

        printf("Title: "); 
        fgets(title, kStringLength, stdin); 
        eliminateEndOfLine(title); 

        if (strcmp(title, ".") == 0) {
            break;
        }

        printf("Artist: "); 
        fgets(artist, kStringLength, stdin);
        eliminateEndOfLine(artist);
            
        printf("Rating: "); 
        while (rating = getNum()) { // error check the rating this will check to make sure the rating is in the range 1-5.

            if (rating<1||rating>5){
                printf("The number you have entered is invaild\n");
                rating = 0; 
                printf("Rating: "); 
                continue; 
            }
            break; 
        }
        head = getSongInfo(head, title, artist, rating); 
    }
    
    printSongInfo(head); 
    return 0;
}

dsA1.h header file

#pragma once

//structs 
typedef struct SongNode {
    char* title;
    char* artist;
    int rating; 
    SongNode* next;
    SongNode* prev; 
}SongNode;

//Constants 
#define kInfiniteLoop 1 
#define kStringLength 30

//Prototypes
SongNode* getSongInfo(SongNode* head, char title[kStringLength], char artist[kStringLength], int rating);
void printSongInfo(SongNode* head); 
int getNum(void); 
void eliminateEndOfLine(char* buffer); 
 

dsA1LL.cpp other source code file

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include "dsA1.h"
#pragma warning(disable : 4996)
#pragma warning(disable : 6387)

SongNode* getSongInfo(SongNode *head, char title[kStringLength], char artist[kStringLength],int rating) {
    
    SongNode* newNode; 
    newNode = (SongNode*)malloc(sizeof(SongNode));
    if (newNode == NULL) {
        printf("No Memory left\n"); 
        return head;
    }

    newNode->title = (char*)malloc(strlen(title) + 1); 
    if (newNode->title == NULL) {
        printf("No memory left for title\n"); 
        return head; 
    }

    newNode->artist = (char*)malloc(strlen(artist) + 1); 
    if (newNode->rating == NULL) {
        printf("No memory left for artist\n"); 
        return head; 
    }

    
    strcpy(newNode->title, title); 
    strcpy(newNode->artist, artist);
    newNode->rating = rating; 

    if (head == NULL) {
        head = newNode;
        return head; 
    }

    head->prev = newNode;
    newNode->next = head; 
    
    return head; 
}

void printSongInfo(SongNode* head) {
    SongNode* ptr;
    ptr = head;

    printf("\n");
    printf("%-35s %-35s\n", "Title", "Artist");

    while (ptr != NULL) {
        printf("%-35s %-35s %-35d\n", ptr->title, ptr->artist, ptr->rating);
        ptr = ptr->next;
    }
}


/*===============================================================================================================*/
/*FUNCTION   :getNum(void)                                                                                       */
/*PARAMETERS :void                                                                                               */
/*RETURNS    :number                                                                                             */
/*DESCRIPTION:This function is the user input function to get a number rating                                    */
/*===============================================================================================================*/
int getNum(void)
{/* the array is 121 bytes in size; we'll see in a later lecture how we can improve this code */
    char record[121] = { 0 }; /* record stores the string */
    int number = 0;
    /* NOTE to student: indent and brace this function consistent with your others */
/* use fgets() to get a string from the keyboard */
    fgets(record, 121, stdin);
    /* extract the number from the string; sscanf() returns a number
 * corresponding with the number of items it found in the string */
    if (sscanf(record, "%d", &number) != 1)
    {
        /* if the user did not enter a number recognizable by
         * the system, set number to -1 */
        number = -1;
    }
    return number;
}



/*=======================================================================================================*/
/*FUCNTION      :void eliminateEndOfLine                                                                 */
/*PARAMETER     :(char* buffer)                                                                          */
/*RETURNS       :void                                                                                    */
/*DESCRIPTION   :This function takes a pointer to a string and looks through the string to find the      */
/*               newline.It takes the new line out of the string.                                        */
/*=======================================================================================================*/
void eliminateEndOfLine(char* buffer)
{
    char* target = strchr(buffer, '\n');
    if (target != NULL)
    {
        *target = '\0';
    }
}

1 Answers1

0

The first node you create doesn't have it's next set to NULL

An easy fix would be move newNode->next = head above the if (head == NULL) check. Though IMHO it's clearer to add newNode->next = NULL inside that check.

Also looks like you add to the list by prepending. You set newNode->next to head and head->prev to newNode - but you never set head to newNode.

Finally: checkout strdup instead of handrolled malloc/strcpy

John3136
  • 28,809
  • 4
  • 51
  • 69
  • This got rid of the unexpected break point but it is only printing the first node of the list. It isn't printing out the entire list. – Erik G Holmes Jun 20 '21 at 18:12
  • Which means your `ptr->next` is null. Manually walk through the process of adding a second entry to the list and see how you fail to update `head->next`. – Kevin Jun 20 '21 at 18:47
  • `strdup` is not [standard ISO C](https://stackoverflow.com/a/32946031/2472827), though arguably useful. – Neil Jun 20 '21 at 20:20