The program I have to write is to create The Nice List for Santa which takes nam
ID: 3854725 • Letter: T
Question
The program I have to write is to create The Nice List for Santa which takes names as input until empty string and gifts for each name until empty string. Then I need to store the names and the gifts associated with each name in a linked list and display it. I have created a class Gift which creates instances in the form if a vector and each vector contains a name and the gifts. I know there is a problem in line 133 and 161 but I cannot figure out. C++
<
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Gift
{
private:
string name;
vector<string>gift;
public:
Gift(){ }
string input;
string getName()
{
cout << "Name for the nice list: ";
getline(cin, name);
return name;
}
void setName(string n)
{
gift.push_back(name);
}
void addGift()
{
cout << "Add gifts for " <<name<<endl;
cout << "Gift: "<<endl;
getline(cin,input);
while (input.length()!=0)
{
gift.push_back(input);
cout << "Gift: " <<endl;
getline(cin,input);
}
}
void displayVectorContents()
{
cout << endl << "The list contains: "<<endl;
for (int i = 0; i <= gift.size(); i++)
{
cout << gift[i] << " ";
}
cout <<endl;
}
friend class GiftNode;
};
class GiftNode
{
private:
Gift item;
GiftNode *next;
friend class LinkedGift;
public:
//GiftNode( );
GiftNode(const Gift& );
void setItem(const Gift&);
void setNext(GiftNode* nextNodePtr);
Gift getItem();
GiftNode* getnext();
};
//GiftNode::GiftNode():next(nullptr)
//{
//} //end default constructor
GiftNode::GiftNode(const Gift& aGift): item(aGift), next(nullptr)
{
} //end 2nd constructor
void GiftNode:: setItem(const Gift& aGift)
{
item = aGift;
} //end setItem
void GiftNode::setNext(GiftNode* nextNodePtr)
{
next = nextNodePtr;
}
Gift GiftNode::getItem()
{
return item;
}
GiftNode* GiftNode::getnext()
{
return next;
}
class LinkedGift
{
private:
GiftNode* headPtr; //pointer to the first node
GiftNode* next; //int itemCount; //current count of bag items
public:
//LinkedGift(); //default constructor
LinkedGift(Gift data);
bool add (const Gift& newGift);
void display();
};
//LinkedGift::LinkedGift() : headPtr(nullptr)
//{
//} //default constructor
LinkedGift::LinkedGift(Gift data)
{
headPtr=new GiftNode(data);
next=headPtr->getNext();
bool add (const Gift& newGift);
void display();
}
bool LinkedGift::add (const Gift& newGift)
{
GiftNode* newNodePtr = new GiftNode(newGift);
newNodePtr->setItem(newGift);
newNodePtr->setNext(headPtr);
headPtr = newNodePtr;
return true;
} //end add
void LinkedGift::LinkedGift:: display()
{
cout <<endl;
GiftNode* temp;
temp = headPtr;
while(temp != nullptr)
{
cout << temp->getItem();
temp = temp-> next;
}
cout << temp->getnext();
cout <<endl;
}
int main()
{
//Gift g;
//LinkedGift g1;
Gift g;
LinkedGift l_g = *new LinkedGift(g);
string name = g.getName();
while (name.length()!= 0)
{
g.setName(name);
g.addGift();
LinkedGift l_g = *new LinkedGift(g);
l_g.add(g);
name = g.getName();
}
l_g.display();
}
#include
#include
#include
using namespace std;
class Gift
{
private:
string name;
vector<string>gift;
public:
Gift(){ }
string input;
string getName()
{
cout << "Name for the nice list: ";
getline(cin, name);
return name;
}
void setName(string n)
{
gift.push_back(name);
}
void addGift()
{
cout << "Add gifts for " <<name<<endl;
cout << "Gift: "<<endl;
getline(cin,input);
while (input.length()!=0)
{
gift.push_back(input);
cout << "Gift: " <<endl;
getline(cin,input);
}
}
void displayVectorContents()
{
cout << endl << "The list contains: "<<endl;
for (int i = 0; i <= gift.size(); i++)
{
cout << gift[i] << " ";
}
cout <<endl;
}
friend class GiftNode;
};
class GiftNode
{
private:
Gift item;
GiftNode *next;
friend class LinkedGift;
public:
//GiftNode( );
GiftNode(const Gift& );
void setItem(const Gift&);
void setNext(GiftNode* nextNodePtr);
Gift getItem();
GiftNode* getnext();
};
//GiftNode::GiftNode():next(nullptr)
//{
//} //end default constructor
GiftNode::GiftNode(const Gift& aGift): item(aGift), next(nullptr)
{
} //end 2nd constructor
void GiftNode:: setItem(const Gift& aGift)
{
item = aGift;
} //end setItem
void GiftNode::setNext(GiftNode* nextNodePtr)
{
next = nextNodePtr;
}
Gift GiftNode::getItem()
{
return item;
}
GiftNode* GiftNode::getnext()
{
return next;
}
class LinkedGift
{
private:
GiftNode* headPtr; //pointer to the first node
GiftNode* next; //int itemCount; //current count of bag items
public:
//LinkedGift(); //default constructor
LinkedGift(Gift data);
bool add (const Gift& newGift);
void display();
};
//LinkedGift::LinkedGift() : headPtr(nullptr)
//{
//} //default constructor
LinkedGift::LinkedGift(Gift data)
{
headPtr=new GiftNode(data);
next=headPtr->getNext();
bool add (const Gift& newGift);
void display();
}
bool LinkedGift::add (const Gift& newGift)
{
GiftNode* newNodePtr = new GiftNode(newGift);
newNodePtr->setItem(newGift);
newNodePtr->setNext(headPtr);
headPtr = newNodePtr;
return true;
} //end add
void LinkedGift::LinkedGift:: display()
{
cout <<endl;
GiftNode* temp;
temp = headPtr;
while(temp != nullptr)
{
cout << temp->getItem();
temp = temp-> next;
}
cout << temp->getnext();
cout <<endl;
}
int main()
{
//Gift g;
//LinkedGift g1;
Gift g;
LinkedGift l_g = *new LinkedGift(g);
string name = g.getName();
while (name.length()!= 0)
{
g.setName(name);
g.addGift();
LinkedGift l_g = *new LinkedGift(g);
l_g.add(g);
name = g.getName();
}
l_g.display();
}
Explanation / Answer
133 Line:
In the above code the method name is getnext() not getNext. ( Here N is capital letter)
next=headPtr->getnext();
161 Line:
Here the method name is getnext() not getItems()
cout << temp->getnext();
Program:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Gift
{
private:
string name;
vector<string>gift;
public:
Gift(){ }
string input;
string getName()
{
cout << "Name for the nice list: ";
getline(cin, name);
return name;
}
void setName(string n)
{
gift.push_back(name);
}
void addGift()
{
cout << "Add gifts for " <<name<<endl;
cout << "Gift: "<<endl;
getline(cin,input);
while (input.length()!=0)
{
gift.push_back(input);
cout << "Gift: " <<endl;
getline(cin,input);
}
}
void displayVectorContents()
{
cout << endl << "The list contains: "<<endl;
for (int i = 0; i <= gift.size(); i++)
{
cout << gift[i] << " ";
}
cout <<endl;
}
friend class GiftNode;
};
class GiftNode
{
private:
Gift item;
GiftNode *next;
friend class LinkedGift;
public:
//GiftNode( );
GiftNode(const Gift& );
void setItem(const Gift&);
void setNext(GiftNode* nextNodePtr);
Gift getItem();
GiftNode* getnext();
};
//GiftNode::GiftNode():next(nullptr)
//{
//} //end default constructor
GiftNode::GiftNode(const Gift& aGift): item(aGift), next(nullptr)
{
} //end 2nd constructor
void GiftNode:: setItem(const Gift& aGift)
{
item = aGift;
} //end setItem
void GiftNode::setNext(GiftNode* nextNodePtr)
{
next = nextNodePtr;
}
Gift GiftNode::getItem()
{
return item;
}
GiftNode* GiftNode::getnext()
{
return next;
}
class LinkedGift
{
private:
GiftNode* headPtr; //pointer to the first node
GiftNode* next; //int itemCount; //current count of bag items
public:
//LinkedGift(); //default constructor
LinkedGift(Gift data);
bool add (const Gift& newGift);
void display();
};
//LinkedGift::LinkedGift() : headPtr(nullptr)
//{
//} //default constructor
LinkedGift::LinkedGift(Gift data)
{
headPtr=new GiftNode(data);
next=headPtr->getnext();
bool add (const Gift& newGift);
void display();
}
bool LinkedGift::add (const Gift& newGift)
{
GiftNode* newNodePtr = new GiftNode(newGift);
newNodePtr->setItem(newGift);
newNodePtr->setNext(headPtr);
headPtr = newNodePtr;
return true;
} //end add
void LinkedGift::LinkedGift:: display()
{
cout <<endl;
GiftNode* temp;
temp = headPtr;
while(temp != nullptr)
{
cout << temp->getnext();
temp = temp-> next;
}
cout << temp->getnext();
cout <<endl;
}
int main()
{
//Gift g;
//LinkedGift g1;
Gift g;
LinkedGift l_g = *new LinkedGift(g);
string name = g.getName();
while (name.length()!= 0)
{
g.setName(name);
g.addGift();
LinkedGift l_g = *new LinkedGift(g);
l_g.add(g);
name = g.getName();
}
l_g.display();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.