Sequence class - HELP!

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 driver file). Please guide me to the right direction. Thanks!

//FILE: sequenceClass.h
//CLASS PROVIDED: sequence (a class for a collection of items)
//
//TYPEDEFS and MEMBER CONSTANTS for the sequence class:
//	typedef____ value_type
//		built-in type (int, char, etc)
//
//	typedef____ size_type
//		
//	static const size_type CAPACITY = ____
//		sequence::CAPACITY is the maximum number of items that a bag can hold.
//
//CONSTRUCTOR for the sequence class:
//	sequence( )
//		postcondition: The sequence has been initialized as an empty bag.
//	
//MODIFICATION MEMBER FUNCTIONS for the sequence class:
//	void start( )
//		postcondition: The function returns the first item in the sequence.
//
//	void advance( )
//		postcondition: The function returns the next item in the sequence.
//	
//	
//	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. In either case, the new item is now
//						the current item of the sequence. 
//
//	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. In either case, the new item is now
//						the current item of the sequence. 
//
//	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.
//
//CONSTANT MEMBER FUNCTIONS for the sequence class:
//	size_type size( ) const
//		postcondition: The return value is the number of items in the sequence.
//
//	bool is_item( ) const
//		postcondition: The function returns a boolean value to indicate whether 
//					   there actually is another item for current to provide
//					   Ture: a valid "current" item.
//					   False: no valid current item.
//
//	value_type current( ) const
//		postcondition: The function returns a item in the sequence.
//
//
//VALUE SEMANTICS for the sequence class:
//	Assignments and the copy constructor may be used with sequence objects.
//
#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
.
-------------------------------------------------------------------------------


//File: sequenceClass.cxx
//CLASS IMPLEMENTED: sequence
//INVARIANT for the sequence class:
//
//

#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;
}



Driver file
------------------------------------------------------------------------------


#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.current( ) << endl;

	 


	system ("pause");
	return 0;
}

.
Last edited on
Don't use namespaces in header files if you can avoid it.

Your constructor first sets "used" to 0, then your for loop inside of the "insert(...)" member function tries to assign the first element of the array to the value of the array position 0 - 1 which is undefined. This would be done everytime "insert(...)" is called which is why you are getting garbage as the first element in your array. If you had used the actual code tags instead of the quote tags I could give you line numbers.

You forgot to include "cstdlib.h" in your "Driver File" is that a typo? Is this supposed to be a proper driver file? If so then for what platform?

EDIT: Also the value for "used" is never reset to 0 after being modified by the member function "insert(...)", so the loop you are using breaks. Maybe you should use a local variable for this instead?
Last edited on
Topic archived. No new replies allowed.