Debug Assertion Failed?

Can someone please help me get through this error???
Program gets item using the item.h file and stores or deletes item using container.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CONTAINER_H
#define CONTAINER_H
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "item.h"
class Container
{
public: 
	Container();
	~Container();
	void add_item(Item item);
	void numberofitems();
	void displayitems();
	void removeitems(int position);

private:
	Item _items[100];
	int _count;
};
#endif 


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
#include "container.h"
Container::Container()
{
	//_items=NULL; Don't know how to set the 100 elements of the the array equal to 0, what I really mean is that I want that memory space to free of junk...
	_count=0;
}

Container::~Container()
{	
	if (_items != (NULL))
	{
		delete [] _items;
	}

}

void Container::add_item(Item item)
{
	_items[_count]= item;
	_count++;
}

void Container::displayitems()
{
	for (int i=0; i < 100; i++)
	{
		if (_items[_count].getId());
	}
}

void Container::removeitems(int pos)
{
	for (int i=pos; i < _count-1; i++)
	{
		_items[i] = _items[i+1];
	}
}
/*void Container::numberofitems()
{
	int a;
	a= _items.size(); // Don't know how to get the size of the private array

}*/


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
#ifndef ITEMH
#define ITEMH
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

class Item
{
public:
	Item();
	Item( int item_id, string desc, double price );
	int getId();
	string getDescription();
	double getPrice();
	string toString();

private:
	int _item_id;
	string _desc;
	double _price;
};
#endif 


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
#include "item.h"

/**************************************************
* Constructors
***************************************************/
Item::Item()
{
	_item_id = 0;
	_price = 0.0;
}

Item::Item( int item_id, string desc, double price )
{
	_item_id = item_id;
	_desc = desc;
	_price = price;
}

/**************************************************
* Accessors
***************************************************/
int Item::getId() { return _item_id; }
string Item::getDescription() { return _desc; }
double Item::getPrice() { return _price; }

/**************************************************
* Convert to string for display (e.g. "(81729) Frozen Peas   $5.63")
***************************************************/
string Item::toString()
{
	// more efficient and flexible than using + to concatenate strings
	ostringstream oss;

	// acts like printing to cout, but stores in a string stream
	oss << "(" << _item_id << ") " << _desc << "\t$" << setprecision(2) << _price;

	// which can then convert to a string
	return oss.str();
}


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
#include <iostream>
#include <string>

#include "item.h"
#include "container.h"
using namespace std;
int main()
{
	Container c; 

	Item i( 1567, "White bread", .99 );
	cout << i.toString() << endl;


	Item i1( 1210, "Milk", 2.99 );
	cout << i1.toString() << endl;


	Item i2( 1435, "Cheese", 3.99 );
	cout << i2.toString() << endl;
	//adding new Item object to the container
	
	c.add_item(i);
	c.add_item(i1);
	c.add_item(i2);
	
        //returning number of Item objects contained
	//c.numberofitems();

	// printing out of all Item objects

	//c.displayitems();
	// removing of an Item in the container by id.
system("PAUSE");
return 0;
}
closed account (zb0S216C)
In "Container::~Container( )" you're deallocating memory that was never allocated. Remove the "delete []" from the destructor. Also, "Container::_items" will never be null since it's an array of "Item"s, not pointers.

Wazzak
Last edited on
Topic archived. No new replies allowed.