How to get pointer array to output data and not memory address?

I'm trying to implement a dynamic bool array in conjuction with a data array that will store items within a bag. The data array stores a character that is provided by the program and the bool array will change its corresponding array member to be true, indicating that the specified space was filled with an item. To simplify what I mean, if data[5] in the bag gains an item, then bool[5] will turn true, to show that the spot is filled. I originally wrote this program with static arrays, and now I'm having trouble turning them into dynamic. In the insert function I have created, that is used to insert an item into the bag, I am currently inserting some cout lines in order to check what the values are at a given point. I keep having the bool array, in_use, only output the number 205, which I assume is the memory address? I need it to output the actual value in the array in the if statement immediately after it. I will post the implementation file and header file.
Header:

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
// FILE: bag_with_receipts2.h
// 
// TYPEDEF and MEMBER CONSTANTS:
//   typedef ____ value_type
//     bag_with_receipts::value_type is the data type of the items in the bag. It may be any of
//     the C++ built-in types (int, char, etc.), or a class with a default
//     constructor, an assignment operator, and operators to
//     test for equality (x == y) and non-equality (x != y).
//
//   typedef ____ size_type
//     bag_with_receipts::size_type is the data type of any variable that keeps track of how many items
//     are in a bag.
//
//   static const size_type DEFAULT_CAPACITY = _____
//     bag_with_receipts::DEFAULT_CAPACITY is the initial capacity of a bag that is created
//     by the default constructor.
//
// CONSTRUCTOR for the bag class:
//   bag_with_receipts(size_type initial_capacity = DEFAULT_CAPACITY)
//     Postcondition: The bag is empty with an initial capacity given by the
//     parameter. The insert function will work efficiently (without
//     allocating new memory) until this capacity is reached.
//
// MODIFICATION MEMBER FUNCTIONS 
//
//   void reserve(size_type new_capacity)
//     Postcondition: The bag's current capacity is changed to the
//     new_capacity (but not less than the number of items already in the
//     bag). The insert function will work efficiently (without allocating
//     new memory) until the new capacity is reached.
//
//	bool remove(int receipt, value_type& itemRemoved)
	/*	"when you want to remove an item,
	you must provide a copy of the receipt as a parameter to the remove function.
	The remove function removes the item whose receipt has been presented,
	and also returns a copy of that item through a reference parameter."	*/
//	Postcondition: If "the item whose receipt has been presented" was in the bag, 
//	then the item has been removed and a copy of that item is returned through 
//	the reference parameter itemRemoved; otherwise the bag is unchanged. 
//	A false return value indicates that nothing was removed; true indicates otherwise.
//
//   void insert(const value_type& item)
	/*	"Each time an item is added to a bag with receipts,
	the insert function returns a unique integer called the receipt."
	"When a new item is added, we will find the first spot that is currently unused
	and store the new item there. The receipt for the item is the index of the location
	where the new item is stored." */
//  Postcondition: A new copy of item has been added to the bag.
//
// CONSTANT MEMBER FUNCTIONS 
//   size_type size( ) const
//     Postcondition: The return value is the total number of items in the bag.
//
// VALUE SEMANTICS for the bag class:
//    Assignments and the copy constructor may be used with bag objects.

#ifndef MAIN_SAVITCH_BAGWRDA_H
#define MAIN_SAVITCH_BAGWRDA_H
#include <cstdlib>  
#include <iostream>
namespace main_savitch_4
{
	class bag_with_receipts
    { 
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef char value_type;
		typedef std::size_t size_type;
		static const size_type DEFAULT_CAPACITY = 10;
		// CONSTRUCTORS and DESTRUCTOR
		bag_with_receipts(std::size_t initial_capacity = DEFAULT_CAPACITY);
		bag_with_receipts(const bag_with_receipts& source);  
		~bag_with_receipts();

        // MEMBER FUNCTIONS
		void operator=(const bag_with_receipts& source);

		void reserve(size_type new_capacity);
		bool remove(int receipt, value_type& itemRemoved);
		std::size_t insert(const value_type& item);

		std::size_t size( ) const { return used; }

		// overloaded << operator 
		friend std::ostream& operator<<(std::ostream& os, const bag_with_receipts& b);

    private:
		value_type *data;     // Pointer to partially filled dynamic array
		bool* in_use;
		std::size_t used;       // How much of array is being used
		std::size_t capacity;   // Current capacity of the bag
    };
}
#endif 


Implementation:

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
#include <cstdlib>
#include <iostream>
#include "bag_with_receipts2.h"

using namespace std;

namespace main_savitch_4
{	

	bag_with_receipts::bag_with_receipts(std::size_t initial_capacity)
	{

		used = 0;//initializes how many many items are in the bag
		capacity = initial_capacity;
		data = new value_type[initial_capacity];
		in_use = new bool[initial_capacity];
		int p = initial_capacity;
	}

	bag_with_receipts::bag_with_receipts(const bag_with_receipts& source)
	{
		in_use = new bool[source.capacity];
		data = new value_type[source.capacity];
		capacity = source.capacity;
		used = source.used;
		copy(source.data, source.data + used, data);
		copy(source.in_use, source.in_use + used, in_use);
	}

	bag_with_receipts::~bag_with_receipts()
	{
		delete[] data;
		delete[] in_use;
	}

	void bag_with_receipts::operator=(const bag_with_receipts& source)
	{

	}

	void bag_with_receipts::reserve(size_type new_capacity)
	{

	}

	bool bag_with_receipts::remove(int receipt, value_type& removedItem)//definition for the insert function
	{
		if (receipt < 100 and receipt > -1)//checks first to make sure that the receipt is not out of bounds
		{
			if (in_use[receipt])//inner check to see if the receipt presented is being used
			{

				in_use[receipt] = false;//and then set the slot the item was in to empty (false)
				removedItem = data[receipt];//used to pass back out a copy of the removed item
				--used;//remove one item from bag size
				return true;//honestly don't know what returning true or false does but I got the 2.5 sooo...
			}
			return false;//returns false if the receipt is not being used
		}
		else
		{
			cout << "Input Error\n";
			return false;//returns false if the receipt value is out of bounds
		}
	}

	std::size_t bag_with_receipts::insert(const value_type& item)//definition for the insert function
	{
		if (size() < 100)//checks to make sure that we aren't about to go over CAPACITY
		{


			for (int loop = 0; loop < 8; loop++)//checks each member of the in_use array
			{
				cout <<  "/ " << *(in_use + loop) << "/\n";
				if (!in_use[loop])//checks to see if the current slot is empty
				{
					cout << "success\n";
					in_use[loop] = true;
					++used;//adds one item to amounts of items in bag 

					data[loop] = item;//inserts the item into the currently empty slot
					return loop;//returns the receipt
					//also check out this CLEAN concatenation
				}
			}

		}
		else
		{
			cout << "Bag is Full!\n";
		}
		return 0;
	}

	std::ostream& operator<<(std::ostream& os, const bag_with_receipts& b)
	{
		return cout << "yeet";
	}


}

Many of my functions have not been written yet, I am simply starting with the insert function as that is what comes first in the given test program.
Line 48: Why is the limit 100? Why not used or capacity?
Line 69: same comment
Line 83: Why 8? Why not 9? or 42? or used? or capacity?

Figure out what the limits of these loops should really be and then adjust the code accordingly.
The limit being 100 on the remove function was from the previous program we wrote where the size was static. I made it 100 then because I could not find out in time how to get it to recognize the capacity variable for some reason. In 69 same answer. And in 83 I had it at 8 because again I couldn't seem to get the program to accept the actual variable of capacity, even though it is declared in the constructor. I just now changed those numbers in the insert function to be capacity, but in the remove function it would be comparing an int type to a size_t type, which is incompatible.
Here is the new code:
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
#include <cstdlib>
#include <iostream>
#include "bag_with_receipts2.h"

using namespace std;

namespace main_savitch_4
{	

	bag_with_receipts::bag_with_receipts(std::size_t initial_capacity)
	{
		used = 0;//initializes how many many items are in the bag
		capacity = initial_capacity;
		data = new value_type[initial_capacity];
		in_use = new bool[initial_capacity];
	}

	bag_with_receipts::bag_with_receipts(const bag_with_receipts& source)
	{
		in_use = new bool[source.capacity];
		data = new value_type[source.capacity];
		capacity = source.capacity;
		used = source.used;
		copy(source.data, source.data + used, data);
		copy(source.in_use, source.in_use + used, in_use);
	}

	bag_with_receipts::~bag_with_receipts()
	{
		delete[] data;
		delete[] in_use;
	}

	void bag_with_receipts::operator=(const bag_with_receipts& source)
	{

	}

	void bag_with_receipts::reserve(size_type new_capacity)
	{

	}

	bool bag_with_receipts::remove(int receipt, value_type& removedItem)//definition for the insert function
	{
		if (receipt < 100 and receipt > -1)//checks first to make sure that the receipt is not out of bounds
		{
			if (in_use[receipt])//inner check to see if the receipt presented is being used
			{

				in_use[receipt] = false;//and then set the slot the item was in to empty (false)
				removedItem = data[receipt];//used to pass back out a copy of the removed item
				--used;//remove one item from bag size
				return true;//honestly don't know what returning true or false does but I got the 2.5 sooo...
			}
			return false;//returns false if the receipt is not being used
		}
		else
		{
			cout << "Input Error\n";
			return false;//returns false if the receipt value is out of bounds
		}
	}

	std::size_t bag_with_receipts::insert(const value_type& item)//definition for the insert function
	{
		if (size() < capacity)//checks to make sure that we aren't about to go over CAPACITY
		{

			for (size_t loop = 0; loop < capacity; loop++)//checks each member of the in_use array
			{
				//cout <<  "/ " << in_use[loop] << "/\n";
				if (!in_use[loop])//checks to see if the current slot is empty
				{
					cout << "success\n";
					in_use[loop] = true;
					++used;//adds one item to amounts of items in bag 

					data[loop] = item;//inserts the item into the currently empty slot
					return loop;//returns the receipt
					//also check out this CLEAN concatenation
				}
			}

		}
		else
		{
			cout << "Bag is Full!\n";
		}
		return 0;
	}

	std::ostream& operator<<(std::ostream& os, const bag_with_receipts& b)
	{
		return cout << "yeet";
	}


}
Last edited on
The code looks good to me now. If it isn't working then post
- a complete program that we can compile
- The input you're using
- The output you get
- The output that you expect to get.

Do you need to expand the bag when it's full?
Topic archived. No new replies allowed.