I'm trying to store the numbers in my driver class by arranging them with insert and attach functions from the implementation file. Insert function is supposed to move the number to the left or a smaller position and attach moves to the right or higher position in the data array.This is the implementation one:
#include "Lab1A.h"
#include <iostream>
#include <cassert>
#include <algorithm>
sequence::sequence() {
used = 0;
current_index = 0;
}
// MUTATOR MEMBER FUNCTIONS
//Postcondition: The first item in the sequence becomes the current item
void sequence::start() {
current_index = 0;
//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() {
if (is_item()) {
current_index++;
}
}
//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 (position 0). In either //case, the newly inserted item is now the current item of the sequence.
void sequence::insert(const value_type& entry) {
if (size() < CAPACITY) {
data[used] = data[used - 1];
data[used] = entry;
data[current_index] = entry;
used++;
}
if (is_item() == false) {
data[used] = entry;
data[used] = data[used + 1];
}
}
//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) {
if (size() < CAPACITY) {
data[used] = data[used + 1];
data[used] = entry;
data[current_index] = entry;
used++;
}
if (is_item() == false) {
data[used] = entry;
data[used] = data[used + 1];
}
}
//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() {
int i;
if (is_item()) {
current_index--;
data[i] = data[current_index];
}
}
// ACCESSOR MEMBER FUNCTIONS
//Postcondition: The value returned is the number of items in the
//sequence.
int sequence::size() const {
return used;
}
//Postcondition: A true return value indicates that there is a valid
//"current" item that may be retrieved by invoking the current
//member function below. A false return value indicates that
//there is no valid current item.
bool sequence::is_item() const {
return (current_index < used);
}
//Precondition: is_item( ) returns true.
//Postcondition: The item returned is the current item in the sequence.
sequence::value_type sequence::current() const {
return data[current_index];
}
void sequence::print() {
for (int j = 0; j < used; j++) {
cout << data[j] << " ";
}
}
Driver file:
#include <iostream>
#include <cstdlib>
#include "Lab1Aimplementation.cpp"
using namespace std;
int main()
{
sequence numbers;
numbers.insert(21);
numbers.attach(33);
numbers.insert(22);
numbers.print();
return 0;
}
I'm trying to get this output: 21 22 33
Instead I get: 22 33 22
Possible declaration of sequence
as OP didn't attach one:
class sequence
{
using index_type = int;
using value_type = size_t;
static const index_type CAPACITY = 1024;
value_type data[CAPACITY];
index_type used;
index_type current_index;
public:
sequence();
void start();
void advance();
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current();
int size() const;
bool is_item() const;
value_type current() const;
void print();
};