Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 645975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:33:41+00:00 2026-05-13T21:33:41+00:00

I am adding musicCD information to a set. I have two different functions for

  • 0

I am adding musicCD information to a set. I have two different functions for this. The problem is adding the musicians. Its only adding the last musician being passed in like its copying over the first ones.

here is the required output to give you an idea of the info. Only ringo starr is adding and not “George Harrison” “John Lennon”.

    -MusicCD-
band:      Beatles
musicians: "George Harrison" "John Lennon" "Ringo Starr"
songs:     10
title:     Sergeant Pepper's Lonely Hearts Club Band
keywords:  acid rock, sixties

here is main()

item = library->addMusicCD("Sergeant Pepper's Lonely Hearts Club Band", "Beatles", 10);
if (item != NULL) {
    library->addKeywordForItem(item, "acid rock");
    library->addKeywordForItem(item, "sixties");
    library->addBandMember(item, "John Lennon");
    library->addBandMember(item, "George Harrison");
    library->addBandMember(item, "Ringo Starr");
    library->printItem(cout, item);
    }

here is the libray.h file where i am adding the info

#include "Library.h"
#include "book.h"
#include "cd.h"
#include "dvd.h"

#include <iostream>
// general functions


ItemSet allBooks;
ItemSet allCDS;
ItemSet allDVDs;
ItemSet* temp;



ItemSetMap allBooksByAuthor;
ItemSetMap allmoviesByDirector;
ItemSetMap allmoviesByActor;

ItemSetMap allMusicByBand;
ItemSetMap allMusicByMusician;

const Item* Library::addMusicCD(const string& title, const string& band, const int nSongs)
{

    ItemSet* obj = new ItemSet();
    CD* item = new CD(title,band,nSongs);

    allCDS.insert(item);
    obj->insert(item);
    //allMusicByBand[band] = obj;

 ItemSetMap::iterator myband = allMusicByBand.find(band);

if(myband != allMusicByBand.end())
{
    myband->second->insert(item);

}
else{
    allMusicByBand.insert(make_pair(band, obj));
}

    return item;

}

void Library::addBandMember(const Item* musicCD, const string& member)
{

    ItemSet* obj = new ItemSet();

    (((CD*) musicCD)->addBandMember(member)); 
    obj->insert((CD*) musicCD);


   ItemSetMap::iterator MByMusician = allMusicByMusician.find(member);

if(MByMusician != allMusicByMusician.end())
{
    MByMusician->second->insert((CD*) musicCD);


}
else
{
    allMusicByMusician.insert(make_pair(member, obj));
}

}

and here is the cd.cpp class

 #include "CD.h"

using namespace std;

CD::CD(const string& theTitle, const string& theBand, const int snumber)
: Item(theTitle), band(theBand),number(snumber)
{



}

CD::~CD()
{
}


const string CD::getBand() const
{
    return band;

}


const string CD::getMusician() const
{
    return musicians;

}

const int CD::getNumber() const
{

    return number;

}

void CD::addBandMember(const string &member)
{


    this->musicians = member;

}


void CD::print(ostream &out) const
{

    out << "-MusicCD-" << endl;
    out << "band: " << this->getBand() << endl;
    out << "musicians: " << this->getMusician() << endl;
    out << "songs: " << this->getNumber() << endl;
    out << "title: " << this->getTitle() << endl;
    out << "keywords: " << this->printKeywords(this->getKeywords()) << endl;
    out << endl;


}

ostream& operator<<(ostream& out, const CD* cd)
{

    cd->print(out);
    return out;

}

finally, here is the cd.h

#ifndef CD_H
#define CD_H
#pragma once
#include "item.h"

class CD : public Item
{
public:

    CD(const string& theTitle, const string& theBand, const int snumber);
    void addBandMember(const string& member);
    const int getNumber() const;
    const string getMusician() const;

    const string getBand() const;
    virtual void print(ostream& out) const;
    ~CD();


private:

    string band;
    string musicians;
    string title;
    int number;

};

ostream& operator<<(ostream& out, const CD* cd);

#endif

I have to assume my problem lies in the Library::addBandMember function?

Keep in mind i cannot change that function in the library class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:33:42+00:00Added an answer on May 13, 2026 at 9:33 pm

    Yes.

    string musicians;
    

    This variable stores a single string. Hence, even though the call succeeds for addBandMember it gets overwritten by the next call. Hence, all you are left with is the name of the last added musician — Ringo in your case. Use a list or vector instead to hold all musicians.

    vector<string> musicians;
    

    and modify addBandMember as:

    void CD::addBandMember(const string &member)
    {
        this->musicians.push_back(member);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.