BaloneyOs BaloneyOs - 1 month ago 6
C++ Question

C++ how to retrieve information from node with multiple parameters

For this project there is a linked list of an individual's information which consists of first name, last name, an integer, and a double. It looks like this:

void DonorList::addDonor(string firstName, string lastName, int memberID, double donation)
{
Node *pNode = new Node(DonorType(firstName, lastName, memberID, donation), nullptr);

if (first == nullptr)
{
first = pNode;
last = pNode;
}
else
{
last->setLink(pNode);
last = pNode;
}
}


It inherits from the DonorType class. What I'm stumped at is how I can match only one parameter out of the 4 in a search function. I've tried the following:

bool DonorList::searchID(int memberNumber)
{
Node *current = first;
bool found = false;
while (current != nullptr || !found)
{
if (current->getDonor() == memberNumber)
{
found = true;
}
}
return found;
}


But of course it doesn't work because I can't compare current->getDonor() (the node of type DonorType) to memberNumber (which is an int). If it's any help, this is the Node class in the header file:

class Node
{
public:
Node(){}
Node(const DonorType& theDonor, Node *theLink)
: donor(theDonor), link(theLink){}
Node* getLink( ) const { return link; }
DonorType getDonor( ) const { return donor; }
void setDonor(const DonorType& theDonor)
{ donor = theDonor; }
void setLink(Node *theLink) { link = theLink; }
private:
DonorType donor;
Node *link; //pointer that points to next node
};


So pretty much right now I'm very lost on how I can access the memberNumber information in the nodes in order to do a search function. Any help would be greatly appreciated, thanks! This project actually has a bunch more files but I did my best to include what I believe is relevant to the issue.

Edit: Here's the DonorTypen class:

class DonorType : public MemberType
{
public:
DonorType();
DonorType(const string& firstName, const string& lastName, const int& memberNumber, const double& donationAmount);

void setDonorInfo(string &firstName, string &lastName, int &memberNumber, double &donationAmount);
void setAmountDonated(double &donationAmount);

double getAmountDonated() const;

void printDonor() const;
void printDonation() const;

~DonorType();

private:
double donation;
};


Edit2: MemberType class

class MemberType
{
public:
MemberType();
MemberType(const string& firstName, const string& lastName, const int& memberNumber);

void setMemberInfo(const string& firstName, const string& lastName, const int& memberNumber);

string getFirstName() const;
string getLastName() const;
int getMembershipNo();

void printName() const;
void printMemberInfo() const;

~MemberType();

private:
string fname;
string lname;
int idnum;
};

Answer

Node::getDonor() returns a DonorType object, which has methods for accessing the individual values, eg:

bool DonorList::searchID(int memberNumber)
{
    Node *current = first;
    while (current)
    {
        if (current->getDonor().getMembershipNo() == memberNumber)
        {
            return true;
        }
        current = current->getLink(); // <-- you also need to add this!
    }
    return false;
}

On a side note, I would suggest changing Node::getDonor() to return a DonerType& reference instead. That way you are not creating copies of data each time getDonor() is called, and also so that things like node->getDonor().set...() will work as expected.

Also, your addDonor() implementation can be simplified to this:

void DonorList::addDonor(string firstName, string lastName, int memberID, double donation)
{
    Node *pNode = new Node(DonorType(firstName, lastName, memberID, donation), nullptr);

    if (!first)
        first = pNode;

    if (last)
        last->setLink(pNode);
    last = pNode;
}
Comments