Can I get some help coding this in C++? I ask for help constantly on this projec
ID: 3844588 • Letter: C
Question
Can I get some help coding this in C++? I ask for help constantly on this project but I have yet to find an answer that actually works. I dont know if I need arrays but here is the song information just in case it might help in some way.
Classic Rock Playlist:
White Snake, Still of the Night-6:22
Rush, Subdivisions-5:33
ACDC, You cant always get what you want-4:50
Def Lepard, Rock of ages-4:13
Jimi Hendrix, Foxey Lady-3:19
ZZ Top, gimme all of your lovin-3:59
The songs are to be a part of the Classic Rock playlist but I wasnt sure if they needed to be in an array or how to do it. My program just needs to be able to work like a regular playlist. Be able to pull up songs and artists in the Classic Rock playlist and Display them when asked.
cin LL classic Rech. Class Cdspla le Cout 77 wat C out 2. Vispla Select Return it cin L41. thin class c Art class classiExplanation / Answer
#include "bst.h"
const int SIZE = 100;
bool again();
int main()
{
char filename[SIZE] = "data.txt";
song_item a_song, found;
table my_music;
char new_title[SIZE]; //temporary for song_info
char new_artist[SIZE];
char new_genre[SIZE];
char key_title[SIZE];
float new_minutes = 0;
int new_plays = 0;
int success;
bool to_cap = true; //for capitolizing the first letter of the title words.
//loads song library into hash table from file.
//returns false if file cannot open or does not work.
ifstream file_in;
file_in.open(filename); //reads in file.
if (!file_in)
{
cout << "Could not open file " << filename << " for reading." << endl;
return 0;
}
//connect to the file and ready to read
file_in.get(new_title, SIZE, ';'); file_in.ignore(SIZE, ';');
while (!file_in.eof()) //previous read was successful.
{
file_in.get(new_artist, SIZE, ';'); file_in.ignore(SIZE, ';');
file_in.get(new_genre, SIZE, ';'); file_in.ignore(SIZE, ';');
file_in >> new_minutes; file_in.ignore(SIZE, ';');
file_in >> new_plays; file_in.ignore(SIZE, ';');
a_song.create_song(new_title, new_artist, new_genre, new_minutes, new_plays);
a_song.display();
my_music.build(a_song);
file_in.get(new_title, SIZE, ';'); file_in.ignore(SIZE, ';');
}
file_in.close();
success = my_music.display();
if (success != 1)
cout << "Unable to display music." << endl;
do
{
cout << "Enter a title: " << endl;
cin.get(key_title, SIZE); cin.ignore(SIZE, ' ');
for (int i = 0; i < strlen(key_title); ++i)
{
if (isalpha(key_title[i]) && to_cap == true)
{
key_title[i] = toupper(key_title[i]);
to_cap = false;
}
else if (isspace(key_title[i]))
to_cap = true;
}
success = my_music.retrieve(key_title, a_song);
if (success != 0)
{
cout << " Here are the songs with the title " << key_title << endl;
a_song.play();
a_song.display();
}
else
cout << " There were no matches." << endl;
}while(again());
return 0;
}
//Asks user if the would like to search for another song.
//Returns true for yes and falser for everything else.
bool again()
{
char responce;
cout << " Enter another title(y/n)? " << endl;
cin >> responce; cin.ignore(SIZE, ' ');
if (responce == 'y' || responce == 'Y')
return true;
else
return false;
}
=====================================================================================================
//Song.cpp
#include "song.h"
//This function initialises the arrays to NULL and the ints to zero.
song_item::song_item()
{
title = NULL;
artist = NULL;
album = NULL;
minutes = 0;
plays = 0;
}
//This function deallocates the arrays.
song_item::~song_item()
{
if (title)
delete [] title;
if (artist)
delete [] artist;
if (album)
delete [] album;
}
//This function take sthe arguments and dynamically allocates
//them into arrays. It returns an int, 0 for failure and 1 for success.
int song_item::create_song(char * new_title, char * new_artist, char * new_album, float new_minutes, int new_plays)
{
if (new_title && new_artist && new_album)
{
title = new char[strlen(new_title) + 1];
strcpy(title, new_title); //deep copy title.
artist = new char[strlen(new_artist) + 1];
strcpy(artist, new_artist); //deep copy artist.
album = new char[strlen(new_album) + 1];
strcpy(album, new_album); //deep copy album;
minutes = new_minutes;
plays = new_plays;
return 1;
}
else
return 0;
}
//This function takes the argument and copies it into the song data members
//it returns an int, 0 for failure and 1 for success.
int song_item::copy_entry( song_item & a_new_song)
{
if (title != NULL)
delete [] title;
if (artist != NULL)
delete [] artist;
if (album != NULL)
delete [] genre; //deallocates any memory owned by the current object.
if (!a_new_song.title || !a_new_song.artist || !a_new_song.genre)
return 0; //check for song
title = new char[strlen(a_new_song.title) + 1];
strcpy(title, a_new_song.title); //deep copy title.
artist = new char[strlen(a_new_song.artist) + 1];
strcpy(artist, a_new_song.artist); //deep copy artist
album = new char[strlen(a_new_song.genre) + 1];
strcpy(genre, a_new_song.genre); //deep copy Genre.
minutes = a_new_song.minutes;
plays = a_new_song.plays;
return 1;
}
int song_item::compare(song_item compare, song_item song_to_add)
{
cout << "comparing song titles" << endl;
if (!compare.title || !song_to_add.title)
{
return 0;
}
if (strcmp(song_to_add.title, compare.title) < 0)
{
return -1;
}
else
{
return 1;
}
}
// This function will return non-zero if the feature sent in as an
//argument matches the data member. It supplies the matching
// back as an argument and increments the plays.
int song_item::retrieve(char * title_to_find, song_item & found)const
{
found.title = new char[strlen(title) + 1];
strcpy(found.title, title);
found.artist = new char[strlen(artist) + 1];
strcpy(found.artist, artist);
found.album = new char[strlen(album) + 1];
strcpy(found.album, album);
found.minutes = minutes;
found.plays = plays;
return 1;
}
int song_item::display() const
{
if (title)
{
cout << endl;
cout << "Title: " << title << endl;
cout << "Artist: " << artist << endl;
cout << "Genre: " << Genre << endl;
cout << "Minutes: " << minutes << endl;
cout << "Number of plays: " << plays << endl;
return 1;
}
else
return 0;
}
int song_item::remove()
{
if (title)
delete [] title;
if (artist)
delete [] artist;
if (Genre)
delete [] Genre;
}
int song_item::play()
{
++plays;
}
==========================================================================================
Song.hpp
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
class song_item
{
public:
song_item();
~song_item();
int create_song(char * new_title, char * new_artist, char * new_genre, float new_minutes, int new_plays);
int copy_entry(song_item & a_new_song);
int retrieve(char * title_to_find, song_item & found)const;
int compare(song_item compare, song_item song_to_add);
int display() const;
int remove();
int play();
private:
char * title;
char * artist;
char * genre;
float minutes;
int plays;
};
============================================================================================================
bst.cpp
#include "bst.h"
//This is the constructor for the table class
table::table()
{
root = NULL;
}
//This is the destructor for the table class.
table::~table()
{
remove_all();
root = NULL;
}
//This function builds the tree adt `by adding nodes starting at the root.
//This is a wrapper function for the recursive function.
//This function takes in a song item and returns an int, 0 for failure, 1 for success
int table::build(song_item & new_song)
{
if (!root)
{
root = new node;
root->song.copy_entry(new_song);
root->left = NULL;
root->right = NULL;
return 1;
}
else
return build(root, new_song);
}
int table::retrieve(char * title_to_find, song_item & found_songs) const
{
return retrieve(title_to_find, found_songs, root);
}
int table::remove(char * title_to_remove)
{
return remove(title_to_remove, root);
}
int table::display() const
{
return display(root);
}
int table::remove_all()
{
return remove_all(root);
}
int table::build(node * root, song_item & to_add)
{
if (!root)
{
root = new node;
root->left = NULL;
root->right = NULL;
root->song.copy_entry(to_add);
cout << "Adding song to leaf" << endl;
root->song.display();
return 1;
}
int result = to_add.compare(root->song, to_add);
if (result == -1)
{
cout << "Root->left" << endl;
build(root->left, to_add);
}
else if (result == 1)
{
cout << "root->right" << endl;
build(root->right, to_add);
}
}
int table::retrieve(char * found_title, song_item found_song, node * root) const
{
/* if (found_song.compare(found_title, root->song.title) == 0)
root->song.copy_entry(found_song);
if (found_song.compare(found_title, root->song.title)== -1)
retrieve( found_title, found_song, root->left);
else
retrieve(found_title, found_song, root->right);
*/
}
int table::display(node * root) const
{
if (root)
{
display(root->left);
root->song.display();
display(root->right);
return 1;
}
}
int table::remove(char * song_title, node * & root)
{
/* if (root->song.compare(root->song.title, song_title)
{
delete root;
root = NULL;
return 1;
}
remove(song_title, root->left);
remove(song_title, root->right);
*/
}
int table::remove_all(node * & root)
{
if (!root)
return 0;
remove_all(root->left);
remove_all(root->right);
delete root;
root = NULL;
return 1;
}
========================================================================
bst.h
#include "song.h"
struct node
{
song_item song;
node * left;
node * right;
};
class table
{
public:
table();
~table();
int build(song_item & new_song);
int retrieve(char * title_to_find, song_item & found_songs) const;
int remove(char * song_title);
int display() const;
int remove_all();
private:
node * root;
int retrieve(char * found_title, song_item found_song, node * root) const;
int build(node * root, song_item & to_add);
int display(node * root) const;
int remove(char * song_title, node * & root);
int remove_all(node * & root);
};
==========================================================================================================
data.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.