please help me on C++ program
Your tasks is to modify BagDyn() class given in BagDynMemAlloc.cpp, to allow up to three duplicates of same item. Initially create a bag with the capacity of 10 items. Ask user to input an item to bag. If the count of current item is already three, then do not add this item to the bag. A sample run given below. A bag with capacity of 10 is ready…
Enter a number: 5
> 5
Enter a number: 3
> 5, 3
Enter a number: 5
> 5, 3, 5
Enter a number: 7
> 5, 3, 5, 7
Enter a number: 5
> 5, 3, 5, 7, 5
Enter a number: 5
Bag has already three
5s. > 5, 3, 5, 7, 5
Enter a number: 1
> 5, 3, 5, 7, 5, 1
Enter a number: 0
> 5, 3, 5, 7, 5, 1, 0
Enter a number: 0
> 5, 3, 5, 7, 5, 1, 0, 0
Enter a number: 0
> 5, 3, 5, 7, 5, 1, 0, 0, 0
Enter a number: 0
Bag has already three 0s.
> 5, 3, 5, 7, 5, 1, 0, 0, 0
A negative user entry quits the program.
Enter a number: -1
Bye..
Assume that user will input numbers from -200 to +200. Raise also message if the bag is overflow, which means user tries to add more than 10 items.
my program:
#include
#include
using namespace std;
struct Node;
struct Node{
string data;
Node *next;
};
class BagDyn
{
public:
void addToList(Node *head){
int i=0,count=0;
string temp;
Node *current;
while (i!=10){
cout << "Enter a Number : "; cin >> temp;
count=countNode(head,temp);
if(count==3)
{
cout<<"Bag alredy has three " <
// the new node is inserted after the empty head
current->next = head->next;
head -> next = current;
i++;
printList(head);
}
}
return;
}
public:
int countNode(Node* head, string search_for)
{
Node* current = head;
int count = 0;
while (current != NULL) {
if (current->data == search_for)
count++;
current = current->next;
}
return count;
}
void printList(Node *head){
Node *current;
// set current to head->next, because the head is empty in your implementation:
current = head->next;
while (current)
{
cout << current->data <<","; current = current->next;
}
cout<
BagDyn bag;
cout << "A bag with capacity of 10 is ready" << endl; bag.addToList(head); return 0; } // this is my program and where i need to add the logic to the program so that whenever a negative user entry quits the program. by displaying Bye..