icremetning / counting objects in a linked list of the same name.

Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

#include <iostream>
#include "item.h"
#include "inventory.h"
#include <stdlib.h>
#include <string.h>
#pragma GCC diagnostic ignored "-Wwrite-strings"

#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

using namespace std;

void AddItem(Inventory& inv,char* name,double weight)
{
	int len = strlen(name)+1;
	char* itemName = new char[len];
	strncpy(itemName,name,len);

	cout << "Adding " << itemName << " with a weight of " << weight << "." << endl;
	inv.AddItem(Item(itemName,weight));

	delete [] itemName;
}

void RemoveItem(Inventory& inv,char* name)
{
	cout << "Removing " << name << "." << endl;
	inv.RemoveItem(name);
}
int main(int argc, const char * argv[])
{
    std::cout << "start" << endl;

	Inventory inv;
    // Make sure printing an empty inventory works
	inv.PrintInventory();

    // Make sure adding the first one works
	AddItem(inv,"helmet",5);

	inv.PrintInventory();

	// Add some more items
	AddItem(inv,"braclet of power",1);
	AddItem(inv,"red potion",2);
	inv.PrintInventory();

    // Add some duplicates
	AddItem(inv,"braclet of power",1);
	inv.PrintInventory();

    // Add some heavy stuff
	AddItem(inv,"bag of gold coins",50);
	AddItem(inv,"bag of gold coins",50);

    // Now some removes
	RemoveItem(inv,"short sword");
	RemoveItem(inv,"helmet");
	RemoveItem(inv,"braclet of power");
	inv.PrintInventory();
    return 0;
}


Item.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "Item.h"
#include <iostream>
#include <iomanip>
#include <string.h>
#pragma warning(disable:4996) 
using namespace std;

Item::Item() {
    name = nullptr;
    weight = 0;
}



Item::Item(char *name, double weight):name(nullptr), weight(weight){

    Item::setName(name);
    
}




char *Item::getName() const
{
    return this->name;
}

Item::Item(const Item& orig) {
}

Item::~Item() {

    if(name)
    {
        delete [] name;
    }
}

void Item::GetName(char *name)const
{
    strcpy(name, this->name);
}

double Item::getWeight() const
{
    return this->weight;
}

void Item::setName(char* name)
{
	if(this->name)
		delete [] this->name;

	this->name = new char[strlen(name)+1];
	strcpy(this->name, name);
}

void Item::setWeight(double weight)
{
    this->weight = weight;
}

void Item::setCount(int count)
{

    this->count = ++count;
}

int Item::getCount() const
{
    return this->count; 
}

ostream& operator<<(ostream& out, const Item& aItem)
{
	out << " ["<< aItem.count << "] " << aItem.name << " " <<aItem.weight << endl;
    return out;
}

const Item& Item::operator=(const Item& aItem)
{
    if(this == &aItem)
    {
        return *this;
    }
       

    else
    {
        setName(aItem.name);
        setWeight(aItem.weight);
    }
}

bool operator==(const Item& item_1, const Item& item_2)
{

    if(strcmp(item_1.getName(), item_2.getName()) == 0)
    {
		return true;

    }
	else
    {
		return false;

    }
}


bool operator!=(const Item& item_1, const Item& item_2)
{

    if(strcmp(item_1.getName(), item_2.getName()) != 0)
    {
        if (item_1.getWeight() != item_2.getWeight()) {
            return true;
        }
    }
	else
		return false;
}

Item.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef __lab1__Item__
#define __lab1__Item__

#include <iostream>

class Item {
public:
    Item();
    Item(char *name, double weight);
    Item(const Item& orig);
    virtual ~Item();

    //Getters
    void GetName(char *name) const;
    double getWeight() const;
    int getCount() const;
    


    //Setters
    void setName(char* name);
    void setWeight(double weight);
    char *getName() const;
    void setCount(int count);


    //Overloading operators
	friend std::ostream& operator<<(std::ostream& out, const Item& aItem);
	const Item& operator=(const Item& aItem);

    
private:
    char *name;
    double weight;
    int count; 

};


//Operators
bool operator< (const Item& item_1, const Item& item_2);
bool operator== (const Item& item_1, const Item& item_2);
bool operator!= (const Item& item_1, const Item& item_2);

#endif /* defined(__lab1__Item__) */ 

Inventory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Inventory.h"

#include <cstdlib>
#include <iostream>
#include "Item.h"

using namespace std; 

Inventory::Inventory()
{
    head = nullptr;
    curr = nullptr;
    temp = nullptr;
}

Inventory::~Inventory()
{

}
void Inventory::AddItem(const Item& addData){
    nodePtr n = new node;
    n->next = nullptr;
    n->aItem = addData;

    if (head != nullptr)
    {
        curr = head;
        while (curr->next != nullptr)
        {
            curr = curr->next;
        }
        curr->next = n;
    }
    else
    {
        head = n;
    }
}

void Inventory::RemoveItem(char *delData){

    nodePtr delPtr = nullptr;
    temp = head;
    curr = head;
    while (curr != nullptr && curr->aItem.getName() != delData) // overload =!
    {
        temp = curr;
        curr = curr->next;
    }
    if (curr == nullptr)
    {
        cout << delData << " was not found i         n the Inventory \n";
        delete delPtr;
    }
    else
    {
        delPtr = curr;
        curr = curr->next;
        temp->next = curr;
        cout << "The value " << delData << " was deleted\n";
        delete delPtr;
    }
}

void Inventory::PrintInventory(){

    cout << "\nCurrent inventory:\n";
    curr = head;
    if (curr == nullptr) {
        cout << "(no items)\n\n";
    }
    while (curr != nullptr)
    {
        cout << "\t"<< curr->aItem;
        curr = curr->next;
    }
    
    
}

Inventory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef __lab1__Inventory__
#define __lab1__Inventory__

#include <iostream>
#include "Item.h"
class Inventory {

private:

typedef struct node
{
    Item aItem;
    node* next;
}* nodePtr;

nodePtr head;
nodePtr curr;
nodePtr temp;

public:

Inventory();
virtual ~Inventory();
void AddItem(const Item& addData);
void RemoveItem(char *delData);
void PrintList();
void PrintInventory();

};


#endif /* defined(__lab1__Inventory__) */ 

output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Current inventory:
(no items)

Adding helmet with a weight of 5.

Current inventory:
	 [0] helmet 5
Adding braclet of power with a weight of 1.
Adding red potion with a weight of 2.

Current inventory:
	 [0] helmet 5
	 [0] braclet of power 1
	 [0] red potion 2
Adding braclet of power with a weight of 1.

Current inventory:
	 [0] helmet 5
	 [0] braclet of power 1
	 [0] red potion 2
	 [0] braclet of power 1
Adding bag of gold coins with a weight of 50.
Adding bag of gold coins with a weight of 50.
Removing short sword.
short sword was not found i         n the Inventory 
Removing helmet.
helmet was not found i         n the Inventory 
Removing braclet of power.
braclet of power was not found i         n the Inventory 

Current inventory:
	 [0] helmet 5
	 [0] braclet of power 1
	 [0] red potion 2
	 [0] braclet of power 1
	 [0] bag of gold coins 50
	 [0] bag of gold coins 50



what I would want to happen is is have my item increment in size/count instead of a new object. How would you handle this algorithmically, you do not have to post code, I just want a push in the right direction.
I see that you have count for items. I assume that you want to use that. What you need to do when you add an item to the inventory is to check first if there is a similar item already there. If there is, you just increment the count. If not, add an item. Similarly, when you delete an item, you check first if the count is 1. If greater, just decrease the count. If 1, you delete the Item
A map is perfect for this. So add it to the private field.

1
2
3
4
5
6
7
8
9
class Inventory {

private:
map<char*,int>inventory;



};


Put name in map and increment int value;

1
2
3
4
5
6
7
8
9
10
11
12
13
void Inventory::AddItem(const Item& addData)
{
    inventory[addData->name]++;
  }

//put in function that displays inventory

map<char*, int>::iterator iter;
for(iter=Inventory.begin();iter!=Inventory.end();iter++)
cout<<iter->first<<"  "<<iter->second; 


Last edited on
I am assuming I need to overload ++ as well to handle the increment logic for map?
Last edited on
I tried to implement a map as Cody suggested, but the program would not compile, would someone mind looking at what he is suggesting closer? And offer any advise on what I may be overlooking?
What are your compiler errors?
Topic archived. No new replies allowed.