tags:

views:

72

answers:

2

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.

+4  A: 

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);
}
dirkgently
A: 

Only ringo starr is adding and not "George Harrison" "John Lennon".

To turn the bug into a feature, rearrange the names so George Harrison is the only one that's added. Then you'll have artificial intelligence so good it has better musical taste than most people!

Seriously though, right now your cd.h has "musicians" as a single string, not a collection of strings. If you want to badly enough, you could put all the musicians in a single string, but at a guess, you'd rather have a vector of strings or something on that order.

Jerry Coffin