Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it. Everything else is fine..
#include <iostream>
#include<string>
using namespace std;
//class Song
class Song{
private:
int songID;
string title;
string artist;
string album;
int year;
public:
Song(); //default constructor, set string variables to "" and set integer variables to 0
Song(int aID, string aTitle, string anArtist, string anAlbum, int aYear); //parametrized constructor
int getSongID()const; // return value song ID
string getTitle()const; // return value title
string getArtist()const; // return value artist
string getAlbum()const; // return value album
int getYearReleased()const; // return value year
void setSongID(int newSongID); //set the songID to newSongID
void setTitle(string newTitle); //set the title to newTitle
void setArtist(string newArtist); //set the artist to newArtist
void setAlbum(string newAlbum); //set album to newAlbum
void setYear(int newYear); //set year to newYear
};
struct node{
Song song;
node *next;
};
//insert your code for all the constructs and member functions of Song class here
//For example, this is the code (incomplete) for the default constructor of the class
Song::Song()
{
songID = 0;
title="";
artist="";
album="";
year=0;
//insert code here
}
Song::Song(int aID, string aTitle, string anArtist, string anAlbum, int aYear){
songID=aID;
title=aTitle;
artist=anArtist;
album=anAlbum;
year=aYear;
}
int Song::getSongID()const{
return songID;
}
string Song::getTitle()const{
return title;
}
string Song::getAlbum()const{
return album;
}
string Song::getArtist()const{
return artist;
}
int Song::getYearReleased()const{
return year;
}
void Song::setSongID(int newSongID){
songID= newSongID;
}
void Song::setTitle(string newTitle){
title= newTitle;
}
void Song::setArtist(string newArtist){
artist = newArtist;
}
void Song::setAlbum(string newAlbum){
album=newAlbum;
}
void Song::setYear(int newYear){
year=newYear;
}
//Complete the code for the following functions
void displayList(node** head);
void displaySong(node** head, int ID);
void insertNewSong(node** head,Song newSong);
int main(int argc, const char * argv[]) {
int choice;
node * list=NULL;
Song newSong;
int newSongID;
string newTitle;
string newArtist;
string newAlbum;
int newYear;
int id;
do
{
cout << endl
<< "***** My Song List *****" << endl
<< " 1 - Display List" << endl
<< " 2 - Display a Song" << endl
<< " 3 - Insert new Song" << endl
<< " 4 - Exit." << endl
<< " Enter your choice and press return: ";
cin >> choice;
switch (choice)
{
case 1:
//call the function displayList to display the song that appears in the list
displayList(&list);
break;
case 2:
{
cout << endl
<< "Enter the ID of the song that you would like to display!" << endl;
cin >> id;
//call displaySong functioin to display the name of the song based on
displaySong(&list, id);
break;
}
case 3:
{
cout << endl
<< "Enter information for your new song!" << endl;
cout << "Song ID : " ;
cin.ignore();
cin >> newSongID;
newSong.setSongID(newSongID);
cout << "Title : " ;
cin.ignore();
getline(cin, newTitle);
newSong.setTitle(newTitle);
cout << "Artist : " ;
getline(cin, newArtist);
newSong.setArtist(newArtist);
cout << "Album : " ;
getline(cin,newAlbum);
newSong.setAlbum(newAlbum);
cout << "Year : " ;
cin >> newYear;
newSong.setYear(newYear);
//call insertNewSong function to insert a new song to the list
//the new song should be added to the FRONT of the list
insertNewSong(&list,newSong);
cout << "New song is added!!!" << endl;
break;
}
case 4:
cout << "End of Program.\n";
break;
default:
cout << "Not a Valid Choice. \n"
<< "Choose again.\n";
break;
}
}while (choice !=4);
return 0;
}
void insertNewSong(node** head,Song newSong){
node * temp= new node;
temp->song= newSong;
temp->next= *head;
*head=temp;
//add your code
}
void displayList(node ** head)
{
node*temp;
temp =*head;
while(temp!=NULL){
//temp->getSongID();
cout<<" Title:" <<temp->song.getTitle()<<endl;
cout<<" Artist:"<<temp->song.getArtist()<<endl;
cout<<" Album:"<<temp->song.getAlbum()<<endl;
cout<<"year:"<<temp->song.getYearReleased()<<endl;
temp=temp->next;
if(temp==NULL){
cout<<"end of the list"<<endl;
return;
}
}
if(temp==NULL){
cout<<" list is empty "<<endl;
}
}
void displaySong(node** head, int ID){
node*temp;
temp =*head;
while(temp!=NULL){
if(ID==temp->song.getSongID()){
cout<<" Title:" <<temp->song.getTitle()<<endl;
cout<<" Artist:"<<temp->song.getArtist()<<endl;
cout<<" Album:"<<temp->song.getAlbum()<<endl;
cout<<"year:"<<temp->song.getYearReleased()<<endl;
}
else{
cout<<" song ID not found "<<endl;
}
temp=temp->next;
return;
}
if(temp==NULL){
cout<<" list is empty"<<endl;
}
}
In the given code newSong variable(Song newSong;) is declared at the top in the main function and object is created when main is loaded. This way there is one object created.
So when case 3 executed again and again same object value is updated.
We should declare variable inside Case 3.
Below is updated code
#include <iostream>
#include<string>
using namespace std;
//class Song
class Song {
private:
int songID;
string title;
string artist;
string album;
int year;
public:
Song(); //default constructor, set string variables to "" and set integer variables to 0
Song(int aID, string aTitle, string anArtist, string anAlbum, int aYear); //parametrized constructor
int getSongID() const; // return value song ID
string getTitle() const; // return value title
string getArtist() const; // return value artist
string getAlbum() const; // return value album
int getYearReleased() const; // return value year
void setSongID(int newSongID); //set the songID to newSongID
void setTitle(string newTitle); //set the title to newTitle
void setArtist(string newArtist); //set the artist to newArtist
void setAlbum(string newAlbum); //set album to newAlbum
void setYear(int newYear); //set year to newYear
};
struct node {
Song song;
node * next;
};
//insert your code for all the constructs and member functions of Song class here
//For example, this is the code (incomplete) for the default constructor of the class
Song::Song()
{
songID = 0;
title = "";
artist = "";
album = "";
year = 0;
//insert code here
}
Song::Song(int aID, string aTitle, string anArtist, string anAlbum, int aYear) {
songID = aID;
title = aTitle;
artist = anArtist;
album = anAlbum;
year = aYear;
}
int Song::getSongID() const {
return songID;
}
string Song::getTitle() const {
return title;
}
string Song::getAlbum() const {
return album;
}
string Song::getArtist() const {
return artist;
}
int Song::getYearReleased() const {
return year;
}
void Song::setSongID(int newSongID) {
songID = newSongID;
}
void Song::setTitle(string newTitle) {
title = newTitle;
}
void Song::setArtist(string newArtist) {
artist = newArtist;
}
void Song::setAlbum(string newAlbum) {
album = newAlbum;
}
void Song::setYear(int newYear) {
year = newYear;
}
//Complete the code for the following functions
void displayList(node ** head);
void displaySong(node ** head, int ID);
void insertNewSong(node ** head, Song newSong);
int main(int argc,
const char * argv[]) {
int choice;
node * list = NULL;
int newSongID;
string newTitle;
string newArtist;
string newAlbum;
int newYear;
int id;
do
{
cout << endl
<<
"***** My Song List *****" << endl
<<
" 1 - Display List" << endl
<<
" 2 - Display a Song" << endl
<<
" 3 - Insert new Song" << endl
<<
" 4 - Exit." << endl
<<
" Enter your choice and press return: ";
cin >> choice;
switch (choice)
{
case 1:
//call the function displayList to display the song that appears in the list
displayList( & list);
break;
case 2:
{
cout << endl
<<
"Enter the ID of the song that you would like to display!" <<
endl;
cin >> id;
//call displaySong functioin to display the name of the song based on
displaySong( & list, id);
break;
}
case 3:
{
Song newSong;
cout << endl
<<
"Enter information for your new song!" << endl;
cout << "Song ID : ";
cin.ignore();
cin >> newSongID;
newSong.setSongID(newSongID);
cout << "Title : ";
cin.ignore();
getline(cin, newTitle);
newSong.setTitle(newTitle);
cout << "Artist : ";
getline(cin, newArtist);
newSong.setArtist(newArtist);
cout << "Album : ";
getline(cin, newAlbum);
newSong.setAlbum(newAlbum);
cout << "Year : ";
cin >> newYear;
newSong.setYear(newYear);
//call insertNewSong function to insert a new song to the list
//the new song should be added to the FRONT of the list
insertNewSong( & list, newSong);
cout << "New song is added!!!" << endl;
break;
}
case 4:
cout << "End of Program.\n";
break;
default:
cout << "Not a Valid Choice. \n"
<<
"Choose again.\n";
break;
}
} while (choice != 4);
return 0;
}
void insertNewSong(node ** head, Song newSong) {
node * temp = new node;
temp - > song = newSong;
temp - > next = * head;
* head = temp;
//add your code
}
void displayList(node ** head)
{
node * temp;
temp = * head;
while (temp != NULL) {
//temp->getSongID();
cout << " Title:" << temp - > song.getTitle() << endl;
cout << " Artist:" << temp - > song.getArtist() << endl;
cout << " Album:" << temp - > song.getAlbum() << endl;
cout << "year:" << temp - > song.getYearReleased() << endl;
temp = temp - > next;
if (temp == NULL) {
cout << "end of the list" << endl;
return;
}
}
if (temp == NULL) {
cout << " list is empty " << endl;
}
}
void displaySong(node ** head, int ID) {
node * temp;
temp = * head;
while (temp != NULL) {
if (ID == temp - > song.getSongID()) {
cout << " Title:" << temp - > song.getTitle() << endl;
cout << " Artist:" << temp - > song.getArtist() << endl;
cout << " Album:" << temp - > song.getAlbum() << endl;
cout << "year:" << temp - > song.getYearReleased() << endl;
} else {
cout << " song ID not found " << endl;
}
temp = temp - > next;
return;
}
if (temp == NULL) {
cout << " list is empty" << endl;
}
}
Hi I've a problem with this code. When I add more than 1 song to the...
I have a C++ code that lets me enter, display and delete a
student record. I need to implement a function that prints the
average grade score of the students I input.
Below is my code and a picture of how my code looks right
now.
#include<iostream>
#include<stdlib.h>
using namespace std;
//Node Declaration
struct node
{
string name;
string id;
int score;
node *next;
};
//List class
class list
{
private:
//head...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...
This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...
Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node { int data; node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() { struct node*head=NULL; char commands; int value; ...
there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 { template<class item> class node { public: typedef item value_type; ...
I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType { int info; nodeType *link; nodeType *current;...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...