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
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?