sequence class

When I compile and run the program, no error is given.It gives me an unexpected result, and the output is
-858993460
. I thought it should give me the first
value of the array (check my main.cpp file). Please guide me to the right direction. Thanks!
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
 
// main.cpp 
#include <iostream>
#include "sequenceClass.h"
using namespace std;

int main()
{
	sequence bag;

	bag.insert(1);
	bag.insert(2);
	bag.insert(3);
	bag.insert(4);
	bag.insert(5);
	bag.start();
	cout << bag << endl;

	return 0;
} 

// sequenceclass.cpp 

#include <iostream>
#include <cassert>
#include <cstdlib>
#include "sequenceClass.h"
using namespace std;

//const sequence::size_type sequence::CAPACITY;

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

sequence::size_type sequence::size() const
{
	return used;
}

void sequence::start()
{
	current_index = 0;
}

sequence::value_type sequence::current() const
{
	return data[current_index];

}

void sequence::advance()
{
	assert(is_item());
	current_index++;
}

//CONSTANT MEMBER FUNCTIONS
bool sequence::is_item() const
{
	return current_index < used;
}

// insert entry before the current item
void sequence::insert(const value_type& entry)
{
	assert(size() < CAPACITY);
	for (int i = used; i > current_index; i--)
	{
		data[i] = data[i - 1];
		data[current_index] = entry;
		used++;
	}
}

// insert entry after the current item
void sequence::attach(const value_type& entry)
{
	assert(size() < CAPACITY);
	for (int i = used; i > current_index; i--)
	{
		data[i] = data[i + 1];
		data[current_index] = entry;
		used++;
	}
}
void sequence::remove_current()
{
	assert(is_item());
	for (int i = current_index + 1; i < used - 1; i++)
	{
		data[i] = data[i + 1];
		used--;
	}
}

ostream& operator<< (ostream& outs, const sequence& source)
{
	outs << source.current() << " ";
	return outs;
}

// sequenceclass.h 

#ifndef SEQUENCECE_CLASS_H
#define SEQUENCECE_CLASS_H

#include <cstdlib>       // Provides size_t 
using namespace std;

class sequence
{
public:
	// TYPEDEFS and MEMBER CONSTANT
	typedef int value_type;
	typedef std::size_t size_type;
	static const size_type CAPACITY = 30;

	//CONSTRUCTOR
	sequence();

	//MODIFICATION MEMBER FUNCTIONS
	void start();
	void advance();
	// insert entry before the current item
	void insert(const value_type& entry);
	// insert entry after the current item
	void attach(const value_type& entry);
	void remove_current();

	//CONSTANT MEMBER FUNCTIONS
	size_type size() const;
	bool is_item() const;
	value_type current() const;
private:
	value_type data[CAPACITY];
	size_type used;
	size_type current_index;
};
//NONMEMBER FUNCTIONS for the point class
ostream& operator<< (ostream& outs, const sequence& source);

#endif 

The problem is in your insert method.

1
2
3
4
5
6
7
8
9
10
11
// insert entry before the current item
void sequence::insert(const value_type& entry)
{
	assert(size() < CAPACITY);
	for (int i = used; i > current_index; i--)
	{
		data[i] = data[i - 1];
		data[current_index] = entry;
		used++;
	}
}


The for loop never goes off to actually insert the entry, because when it reaches the for loop, used == 0 and current_index == 0.

The for loop will only go off if i > current_index, but 0 will never be greater than 0. I'm not sure what you're trying to do with your insert method, but you should review the logic in that method, because that's where your issue is.
Topic archived. No new replies allowed.