Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include using namespace std; #include PlaylistNode.h PlaylistNode::PlaylistNode() { this ->uniqueID = none ; this ->songName = none ; this ->artistName = none ; this

#include  #include using namespace std; #include"PlaylistNode.h" PlaylistNode::PlaylistNode() { this->uniqueID ="none"; this->songName = "none"; this->artistName = "none"; this->songLength = 0; this->nextNodePtr = 0; } PlaylistNode::PlaylistNode(string initID, string initSongName, string initArtistName, int initSongLength, PlaylistNode* nextLoc) { this->uniqueID = initID; this->songName = initSongName; this->artistName = initArtistName; this->songLength = initSongLength; this->nextNodePtr = nextLoc; } void PlaylistNode::InsertAfter(PlaylistNode* nodePtr) { PlaylistNode* tmpNext = 0; tmpNext = this->nextNodePtr; this->nextNodePtr =nodePtr; nodePtr->nextNodePtr = tmpNext; return; } void PlaylistNode::SetNext(PlaylistNode* nodePtr) { this->nextNodePtr = nodePtr; return; } string PlaylistNode:: GetID() const { return this->uniqueID; } string PlaylistNode:: GetSongName() const { return this->songName; } string PlaylistNode::GetArtistName() const { return this->artistName; } int PlaylistNode::GetSongLength() const { return this->songLength; } PlaylistNode* PlaylistNode::GetNext() { return this->nextNodePtr; } void PlaylistNode::PrintPlaylistNode() { cout << "Unique ID: " << this->uniqueID << endl; cout << "Song Name: " << this->songName << endl; cout << "Artist Name: " << this->artistName << endl; cout << "Song Length (in seconds): " << this->songLength << endl; return; } 

C++ Programming. Can someone please edit my program that I have so far and point out the errors? Can someone please check if it runs correctly?

Here is the instructions for the programming assignment:

You will be building a linked list. Make sure to keep track of both the head and tail nodes.

(1) Create three files to submit.

PlaylistNode.h - Class declaration (use file provided)

PlaylistNode.cpp - Class definition

main.cpp - main() function

Build the PlaylistNode class per the following specifications.

Default constructor

Parameterized constructor

Public member functions

o InsertAfter()

o SetNext() - Mutator

o GetID() - Accessor

o GetSongName() - Accessor

o GetArtistName() - Accessor

o GetSongLength() - Accessor

o GetNext() - Accessor

o PrintPlaylistNode()

Private data members

o string uniqueID - Initialized to "none" in default constructor

o string songName - Initialized to "none" in default constructor

o string artistName - Initialized to "none" in default constructor

o int songLength - Initialized to 0 in default constructor

o PlaylistNode* nextNodePtr - Initialized to 0 in default constructor

Note: Some functions can initially be function stubs (empty functions), to be completed in later steps.

(2) In main(), prompt the user for the title of the playlist. Ex:

Enter playlist's title: JAMZ

(3) Implement the PrintMenu() function. PrintMenu() takes the playlist title as a parameter and outputs a menu of options to manipulate the playlist. Each option is represented by a single character. Build and output the menu within the function.

If an invalid character is entered, continue to prompt for a valid choice.

Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to execute the menu until the user enters q to Quit.

Ex:

JAMZ PLAYLIST MENU

a - Add song

d - Remove song

c - Change position of song

s - Output songs by specific artist

t - Output total time of playlist (in seconds)

o - Output full playlist

q - Quit

Choose an option:

(4) Implement "Output full playlist" menu option. If the list is empty, output: Playlist is empty

Ex:

JAMZ - OUTPUT FULL PLAYLIST

1.

Unique ID: SD123

Song Name: Peg

Artist Name: Steely Dan

Song Length (in seconds): 237

2.

Unique ID: JJ234

Song Name: All For You

Artist Name: Janet Jackson

Song Length (in seconds): 391

3.

Unique ID: J345

Song Name: Canned Heat

Artist Name: Jamiroquai

Song Length (in seconds): 330

4.

Unique ID: JJ456

Song Name: Black Eagle

Artist Name: Janet Jackson

Song Length (in seconds): 197

5.

Unique ID: SD567

Song Name: I Got The News

Artist Name: Steely Dan

Song Length (in seconds): 306

(5) Implement the "Add song" menu item. New additions are added to the end of the list. Ex:

ADD SONG

Enter song's unique ID: SD123

Enter song's name: Peg

Enter artist's name: Steely Dan

Enter song's length (in seconds): 237

(6) Implement the "Remove song" function. Prompt the user for the unique ID of the song to be removed.

Ex:

REMOVE SONG

Enter song's unique ID: JJ234

"All For You" removed

(7) Implement the "Change position of song" menu option. Prompt the user for the current position of the song and the desired new position. Valid new positions are 1 - n (the number of nodes). If the user enters a new position that is less than 1, move the node to the position 1 (the head). If the user enters a new position greater than n, move the node to position n (the tail). 6 cases will be tested:

Moving the head node

Moving the tail node

Moving a node to the head

Moving a node to the tail

Moving a node up the list

Moving a node down the list

Ex:

CHANGE POSITION OF SONG

Enter song's current position: 3

Enter new position for song: 2

"Canned Heat" moved to position 2

(8) Implement the "Output songs by specific artist" menu option. Prompt the user for the artist's name, and output the node's information, starting with the node's current position. Ex:

OUTPUT SONGS BY SPECIFIC ARTIST

Enter artist's name: Janet Jackson

2.

Unique ID: JJ234

Song Name: All For You

Artist Name: Janet Jackson

Song Length (in seconds): 391

4.

Unique ID: JJ456

Song Name: Black Eagle

Artist Name: Janet Jackson

Song Length (in seconds): 197

(9) Implement the "Output total time of playlist" menu option. Output the sum of the time of the playlist's songs (in seconds). Ex:

OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)

Total time: 1461 seconds

Here is what I have so far.

?PLAYLISTNODE.H

#ifndef PLAYLIST_H #define PLAYLIST_H #include  using namespace std; class PlaylistNode { public: PlaylistNode(); PlaylistNode(string initID, string initSongName, string initArtistName, int initSongLength, PlaylistNode* nextLoc = 0); void InsertAfter(PlaylistNode* nodePtr); void SetNext(PlaylistNode* nodePtr); string GetID() const; string GetSongName() const; string GetArtistName() const; int GetSongLength() const; PlaylistNode* GetNext(); void PrintPlaylistNode(); private: string uniqueID; string songName; string artistName; int songLength; PlaylistNode* nextNodePtr; }; #endif 

MAIN.CPP

?

#include  #include  using namespace std; #include "PlaylistNode.h" using namespace std; void PrintMenu(const string playlistTitle) { char menuOp = ' '; string uniqueID; string songName; string artistName; int songLength = 0; int numNodes = 0; int songPosition = 0; int newPosition = 0; int totalTime; PlaylistNode* newSong = 0; PlaylistNode* currNode = 0; PlaylistNode* songNode = 0; PlaylistNode* prevNode = 0; PlaylistNode* insertPosNode = 0; PlaylistNode* headNode = 0; PlaylistNode* tailNode = 0; PlaylistNode* currPrintNode = 0; // Output menu option, prompt users for valid selection  while(menuOp != 'q') { menuOp = ' '; cout << playlistTitle << " PLAYLIST MENU" << endl; cout << "a - Add song" << endl; cout << "d - Remove song" << endl; cout << "c - Change position of song" << endl; cout << "s - Output songs by specific artist" << endl; cout << "t - Output total time of playlist (in seconds)" << endl; cout << "o - Output full playlist" << endl; cout << "q - Quit" << endl << endl; while (menuOp != 'a' && menuOp != 'd' && menuOp != 'c' && menuOp != 's' && menuOp != 't' && menuOp != 'o' && menuOp != 'q') { cout << "Choose an option: "; cin >> menuOp; cout << endl; } // Call corresponding menu action  switch (menuOp) { case 'a': // Prompt user for song information  cout << "ADD SONG" << endl; cout << "Enter song's unique ID: "; cin >> uniqueID; cout << endl; cin.ignore(); cout << "Enter song's name: "; getline(cin, songName); cout << endl; cout << "Enter artist's name: "; getline(cin, artistName); cout << endl; cout << "Enter song's length (in seconds): "; cin >> songLength; cout << endl; // Create a new node for playlist  newSong = new PlaylistNode(uniqueID, songName, artistName, songLength); // If song is first in playlist, update head/tail // Otherwise insert to end of playlist and update tail  if (headNode == 0) { headNode = newSong; tailNode = newSong; } else { tailNode->InsertAfter(newSong); tailNode = tailNode->GetNext(); } break; case 'd': // Output playlist messaging  cout << "REMOVE SONG" << endl; cin.ignore(); cout << "Enter song's unique ID: "; getline(cin, uniqueID); break; case 'c': // Prompt user to new song location  cout << "CHANGE POSITION OF SONG" << endl; cout << "Enter song's current position: " << endl; cin >> songPosition; cout << endl; cout << "Enter new position for song: " << endl; cin >> newPosition; cout << endl; songNode = headNode; numNodes = 0; while ((songNode != 0) && (numNodes < (songPosition -1))) { ++numNodes; songNode = songNode->GetNext(); } if (songNode == 0) { } else { if(songNode == headNode){ headNode = songNode->GetNext(); } else { prevNode = headNode; while (prevNode->GetNext() != songNode) { prevNode = prevNode->GetNext(); } prevNode->SetNext(songNode->GetNext()); if(songNode == tailNode) { tailNode = prevNode; } } if(newPosition <= 1) { songNode ->SetNext(headNode); headNode = songNode; cout << "\"" << songNode -> GetSongName() << "\" "moved to position 1" << endl << endl;  } else { insertPosNode = headNode; numNodes = 0; while ((insertPosNode != 0 && ((numNodes + 2) < newPosition))){ insertPosNode = insertPosNode->GetNext(); numNodes ++; } if (insertPosNode == 0) { tailNode -> InsertAfter(songNode); tailNode = songNode; newPosition = numNodes + 1; } else { insertPosNode->InsertAfter(songNode); newPosition = numNodes + 2; } if (tailNode = insertPosNode) { tailNode = songNode; } cout << "\"" << songNode ->GetSongName() << "\" moved to position " << newPosition << endl << endl; } } break; case 's': // Consume newline and prompt user for output criteria  cin.ignore(); cout << "OUTPUT SONGS BY SPECIFIC ARTIST" << endl; break; case 't': // Output playlist messaging  cout << "OUTPUT TOTAL TIME OF PLAYLIST (IN SECONDS)" << endl; break; case 'o': // Output playlist messaging  cout << playlistTitle << " - OUTPUT FULL PLAYLIST" << endl; // Iterate through each song in list  numNodes = 1; currPrintNode = headNode; // If list is empty, output error message  if (headNode == 0) { cout << "Playlist is empty" << endl << endl; } // Otherwise call print function for each node in list  else { while (currPrintNode != 0) { cout << numNodes << "." << endl; currPrintNode->PrintPlaylistNode(); currPrintNode = currPrintNode->GetNext(); cout << endl; ++numNodes; } } break; } } } int main() { string playlistTitle; // Prompt user for playlist title  cout << "Enter playlist's title: "; getline(cin, playlistTitle); cout << endl; // Output play list menu options  PrintMenu(playlistTitle); return 0; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions