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';
}
}