C++ Sequence class error

When I try to run the program, I eventually get a message saying "assertion failed" so I figure there is a problem with attach(), is_item(), and/or advance() but I'm not sure what.

Here is my header file:

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// FILE: sequence1.h
// CLASS PROVIDED: sequence (part of the namespace main_savitch_3)
// There is no implementation file provided for this class since it is
// an exercise from Section 3.2 of "Data Structures and Other Objects Using C++"
//
// TYPEDEFS and MEMBER CONSTANTS for the sequence class:
//   typedef ____ value_type
//     sequence::value_type is the data type of the items in the sequence. It
//     may be any of the C++ built-in types (int, char, etc.), or a class with a
//     default constructor, an assignment operator, and a copy constructor.
//
//   typedef ____ size_type
//     sequence::size_type is the data type of any variable that keeps track of
//     how many items are in a sequence.
//
//   static const size_type CAPACITY = _____
//     sequence::CAPACITY is the maximum number of items that a sequence can hold.
//


#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib>  // Provides size_t
#include <cassert>	//for assert function
#include <iostream>	//for test function

using namespace std;

namespace main_savitch_3
{
	class sequence
	{
	public:
		// TYPEDEFS and MEMBER CONSTANTS
		typedef double value_type;
		typedef std::size_t size_type;
		static const size_type CAPACITY = 30;
		// CONSTRUCTOR
		sequence();
		// MODIFICATION MEMBER FUNCTIONS
		void start();
		void advance();
		void insert(const value_type& entry);
		void attach(const value_type& entry);
		void remove_current();
		// CONSTANT MEMBER FUNCTIONS
		size_type size() const { return used; }
		bool is_item() const;
		value_type current() const;
		void test() const;
	private:
		value_type data[CAPACITY];
		size_type used;
		size_type current_index;
	};

	sequence::sequence()
	{
		used = 0;
		current_index = 0;
	}

	//   void insert(const value_type& entry)
	//     Precondition: size( ) < CAPACITY.
	//     Postcondition: A new copy of entry has been inserted in the sequence
	//     before the current item. If there was no current item, then the new entry 
	//     has been inserted at the front of the sequence. In either case, the newly
	//     inserted item is now the current item of the sequence.
	void sequence::insert(const value_type& entry)
	{
		assert(size() < CAPACITY);

		for (size_type i = used; i > current_index; i--)
		{
			data[i] = data[i - 1];
		}
		data[current_index] = entry;
		used++;

	}

	//   void start( )
	//     Postcondition: The first item on the sequence becomes the current item
	//     (but if the sequence is empty, then there is no current item).
	void sequence::start()
	{
		current_index = 0;
	}

	//   value_type current( ) const
	//     Precondition: is_item( ) returns true.
	//     Postcondition: The item returned is the current item in the sequence.
	sequence::value_type sequence::current() const
	{
		assert(is_item());
		return data[current_index];
	}

	//   void advance( )
	//     Precondition: is_item returns true.
	//     Postcondition: If the current item was already the last item in the
	//     sequence, then there is no longer any current item. Otherwise, the new
	//     current item is the item immediately after the original current item.
	void sequence::advance()
	{
		assert(is_item());
		current_index++;
	}
        //bool is_item() const
        // Postcondition: A true return value indicates that there is a valid "current" item that
        // may be retrieved by the current member function (listed below). A false return value
        // indicated that there is no current item.

	bool sequence::is_item() const
	{
		
		bool found = false;
		if (current_index < used)
			found = true;
		return found;
		//The below code is  an alternative
		//return (current_index < used);
	}

	//   void attach(const value_type& entry)
	//     Precondition: size( ) < CAPACITY.
	//     Postcondition: A new copy of entry has been inserted in the sequence after
	//     the current item. If there was no current item, then the new entry has 
	//     been attached to the end of the sequence. In either case, the newly
	//     inserted item is now the current item of the sequence.
	void sequence::attach(const value_type& entry)
	{
		assert(size() < CAPACITY);
	
		if (!is_item())
			current_index = used - 1;
		else 
		{
			for (size_type i = used; i > current_index + 1; --i)
				data[i] = data[i - 1];
		}
		current_index++;
		data[current_index] = entry;
		used++;
		
	}

	//   void remove_current( )
	//     Precondition: is_item returns true.
	//     Postcondition: The current item has been removed from the sequence, and the
	//     item after this (if there is one) is now the new current item.
	void sequence::remove_current()
	{
		assert(is_item());

		for (size_type i = current_index + 1; i < used; ++i)
			data[i - 1] = data[i];
		used--;
		if(used==0)
			current_index=used;
	}

	void sequence::test() const
	{
		cout << "The current index is: " << current_index << endl;
		cout << "{ ";
		for (size_t i = 0; i < used; i++)		//used is of type size_t which the computer reads as an unsigned integer
			cout << data[i] << ", ";
		cout << "} used = " << used << endl;
	}

}




#endif 


My main file is too long, but the problem I'm getting is from this section of the 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
sequence test;
	size_t i;

	// Put three items in the sequence
	cout << "Using attach to put 20 and 30 in the sequence, and then calling\n";
	cout << "advance, so that is_item should return false ... ";
	cout.flush();
	test.attach(20);
	test.attach(30);
	test.advance();
	if (test.is_item())
	{
		cout << "failed." << endl;
		return 0;
	}
	cout << "passed." << endl;

	// Insert 10 at the front and run the cursor off the end again
	cout << "Inserting 10, which should go at the sequence's front." << endl;
	cout << "Then calling advance three times to run cursor off the sequence ...";
	cout.flush();
	test.insert(10);
	test.advance(); // advance to the 20
	test.advance(); // advance to the 30
	test.advance(); // advance right off the sequence
	if (test.is_item())
	{
		cout << " failed." << endl;
		return false;
	}
	cout << " passed." << endl;

	// Attach more items until the sequence becomes full.
	// Note that the first attach should attach to the end of the sequence.
	cout << "Calling attach to put the numbers 40, 50, 60 ...";
	cout << test.CAPACITY * 10 << " at the sequence's end." << endl;
	for (i = 4; i <= test.CAPACITY; i++)
		test.attach(i * 10);

	// Test that the sequence is correctly filled.
	cout << "Now I will test that the sequence has 10, 20, 30, ...";
	cout << test.CAPACITY * 10 << "." << endl;
	test.start();
	for (i = 1; i <= test.CAPACITY; i++)
	{
		if ((!test.is_item()) || test.current() != i * 10)
		{
			cout << "    Test failed to find " << i * 10 << endl;
			return 0;
		}
		test.advance();
	}
	if (test.is_item())
	{
		cout << "    There are too many items on the sequence." << endl;
		return false;
	}

	// All tests passed
	cout << "All tests of this second function have been passed." << endl;
	return POINTS[2];


I'm getting an assertion failed error at around line 25 of my test code. I'm not sure what's happening. I tried debugging, but it gave me no new info.
I'd appreciate all help.


Last edited on
The assert that is triggered is on line 106, inside the advance() function.

Note that current_index will hold the position of the newly inserted value after using attach() so after insert(10) on line 22 you are already at the end of the sequence.
Topic archived. No new replies allowed.