Hi,

I am working on this assignment and I have tried EVERYTHING, but to no avail.

I'm working on this stack and queue fusion called quack and the memory is driving me CRAZY! In the output, it displays actual numbers for pushFront and pushBack. But for popFront and popBack functions, it displays garbage.

What I am trying to do is to fix that garbage so that it can be a real number. I have tried declaring the memory and nothing happened. My brother tried to help and we spent hours on it. Even tutors, who are SUPPOSED to be helpful have no clue. I am completely out of ideas!

I just need someone to fix this for me so that I can work on the rotation and reverse functions next, because I don't want to work on this again. I don't want hints! Just answers. Any help is appreciated. Thank you.

Here are the codes:

quack.h (the header file for quack) - The only thing I added under the private section were front, back, count, and capacity.
Code:
// you can only change this file by adding items to the "private" section

#pragma once

#include <ostream>

using namespace std;

class quack
{
public:
	quack(int capacity);
	~quack(void);
	bool pushFront(const int n);	// push an item onto the front
	bool pushBack(const int n);		// push an item onto the back
	bool popFront(int& n);			// pop an item off the front
	bool popBack(int& n);			// pop an item off the back
	void rotate(int r);				// "rotate" the stored items (see note below)
	void reverse(void);				// reverse the order of the stored items
	int	itemCount(void);			// return the current number of stored items

private:
	struct item						// definition of each item stored by the quack
	{
		int		n;
	};

	item		      *items;				// pointer to storage for the circular array
	int			front;				// first item in the quack
	int			back;				// last item in the quack
	int			count;				// Counts how many items in the quack
	int			capacity;			// The max number of the quack

public:
	friend ostream& operator<<(ostream& out, quack& q);
	friend ostream& operator<<(ostream& out, quack::item& i);
};

// the following example should make it clear what "rotate" means:
//   quack: 2, 1, 0, 7, 8		-- current contents, front is on the left
//   >>> rotate(2)
//   quack: 0, 7, 8, 2, 1		-- contents after rotation by +2
//   >>> rotate(-3)
//   quack: 8, 2, 1, 0, 7		-- contents after rotation by -3
quack.cpp (the code I am working on) - again I haven't done the rotation and reverse functions yet.
Code:
// add code to these functions to make quack do something useful

// enable Visual C++ memory leak checking
#ifdef _DEBUG
#include <ostream>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

#include "quack.h"

/*********************
private:
item		      *items;		// pointer to storage for the circular array
int			front;	        // index of the first item in the quack
int			back;	        // index of the last item in the quack
int			count;	        // Counts how many items in the quack
const int	        capacity;		// The max number of the quack
*********************/

quack::quack(int capacity) : 
	capacity(capacity), 
	front(capacity - 1), 
	back(capacity), 
	count(0), 
	items(new item[capacity])
{
}

quack::~quack(void)
{
	delete [] items;
}

bool quack::pushFront(const int n)
{	

	if (count == capacity)
		return false;
	else
		front = (front - 1) % capacity;
		items[front].n = n;
		count++;
		return true;
}

bool quack::pushBack(const int n)
{

	if (count == capacity)
		return false;
	else
		back = (back = front + count) % capacity;
		items[back].n = n;
		count++;
		return true;
}

bool quack::popFront(int& n)
{
	front = (front + 1) % capacity;
	--count;
	return true;
}

bool quack::popBack(int& n)
{
	back = (back - 1) % capacity;
	--count;
	return true;
}

void quack::rotate(int r)
{
}

void quack::reverse(void)
{
}

int	quack::itemCount(void)
{
	return count;
}

ostream& operator<<(ostream& out, quack& q)
{
	// alter this function as needed to produce the required output

  if ( q.count == 0 ) // no elements have been counted.
    out << endl << "quack: empty" << endl;
    else
    {
        out << endl << "quack: ";
        for ( int i = 1; i < q.count + 1; i++ )
        {
               
        out << q.items[i] << ", ";
               
        }
        out << endl << endl;
    }
    return out;

}

ostream& operator<<(ostream& out, quack::item& i)
{
out << i.n;
	return out;
}
Here's the driver code to run the program. It's readable-only
Code:
// make no changes to this file except to have it
// print your own name instead of "Your Name"

// enable Visual C++ memory leak checking
#ifdef _DEBUG
#include <ostream>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

#include <iostream>
#include "quack.h"

using namespace std;

const static int QUACK_SIZE = 7;

static void push(quack& q, bool front, int n)
{
	bool	ok;

	if (front)
		ok = q.pushFront(n);
	else
		ok = q.pushBack(n);
	cout << ">>> push" << (front ? "Front " : "Back ") << n << (ok ? " succeeded\n" : " failed\n");
}

static void pop(quack& q, bool front)
{
	bool	ok;
	int		n;

	if (front)
		ok = q.popFront(n);
	else
		ok = q.popBack(n);
	cout << ">>> pop" << (front ? "Front " : "Back ");
	if (ok)
		cout << "succeeded: " << n << endl;
	else
		cout << "failed\n";
}

int main (void)
{
	quack	q(QUACK_SIZE);

#ifdef _WIN32
	// request memory leak report in Output Window after main returns
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

	cout << "CS260 - Lab2 - Christian Rhodes\n";
	cout << q;
	push(q, true, 1);
	push(q, true, 2);
	push(q, true, 3);
	push(q, true, 4);
	push(q, false, 0);
	push(q, true, 9);
	cout << q;
	cout << "--- # of items: " << q.itemCount() << endl << endl;
	pop(q, true);
	cout << q;
	pop(q, true);
	cout << q;
	push(q, false, 7);
	cout << q;
	push(q, false, 8);
	cout << q;
	cout << ">>> rotate(2)\n";
	q.rotate(2);
	cout << q;
	cout << ">>> rotate(-3)\n";
	q.rotate(-3);
	cout << q;
	cout << ">>> reverse\n";
	q.reverse();
	cout << q;
	push(q, true, 6);
	cout << q;
	cout << ">>> rotate(3)\n";
	q.rotate(3);
	cout << q;
	cout << ">>> rotate(-4)\n";
	q.rotate(-4);
	cout << q;
	cout << "--- # of items: " << q.itemCount() << endl << endl;
	while (q.itemCount() > 0) {
		pop(q, false);
		cout << q;
		}
	cout << "--- # of items: " << q.itemCount() << endl << endl;

	return 0;
}
Here's the output. This is what the output suppose to show
Code:
CS260 - Lab2 - Your Name

quack: empty

>>> pushFront 1 succeeded
>>> pushFront 2 succeeded
>>> pushFront 3 succeeded
>>> pushFront 4 succeeded
>>> pushBack 0 succeeded
>>> pushFront 9 succeeded

quack: 9, 4, 3, 2, 1, 0

--- # of items: 6

>>> popFront succeeded: 9

quack: 4, 3, 2, 1, 0

>>> popFront succeeded: 4

quack: 3, 2, 1, 0

>>> pushBack 7 succeeded

quack: 3, 2, 1, 0, 7

>>> pushBack 8 succeeded

quack: 3, 2, 1, 0, 7, 8

>>> rotate(2)

quack: 1, 0, 7, 8, 3, 2

>>> rotate(-3)

quack: 8, 3, 2, 1, 0, 7

>>> reverse

quack: 7, 0, 1, 2, 3, 8

>>> pushFront 6 succeeded

quack: 6, 7, 0, 1, 2, 3, 8

>>> rotate(3)

quack: 1, 2, 3, 8, 6, 7, 0

>>> rotate(-4)

quack: 8, 6, 7, 0, 1, 2, 3

--- # of items: 7

>>> popBack succeeded: 3

quack: 8, 6, 7, 0, 1, 2

>>> popBack succeeded: 2

quack: 8, 6, 7, 0, 1

>>> popBack succeeded: 1

quack: 8, 6, 7, 0

>>> popBack succeeded: 0

quack: 8, 6, 7

>>> popBack succeeded: 7

quack: 8, 6

>>> popBack succeeded: 6

quack: 8

>>> popBack succeeded: 8

quack: empty

--- # of items: 0
But, this is what I'm getting in the output instead of the above output.
Code:
CS260 - Lab2 - Christian Rhodes

quack: empty
>>> pushFront 1 succeeded
>>> pushFront 2 succeeded
>>> pushFront 3 succeeded
>>> pushFront 4 succeeded
>>> pushBack 0 succeeded
>>> pushFront 9 succeeded

quack: 9, 4, 3, 2, 1, 0,

--- # of items: 6

>>> popFront succeeded: -858993460

quack: 9, 4, 3, 2, 1,

>>> popFront succeeded: -858993460

quack: 9, 4, 3, 2,

>>> pushBack 7 succeeded

quack: 9, 4, 3, 2, 1,

>>> pushBack 8 succeeded

quack: 8, 4, 3, 2, 1, 0,

>>> rotate(2)

quack: 8, 4, 3, 2, 1, 0,

>>> rotate(-3)

quack: 8, 4, 3, 2, 1, 0,

>>> reverse

quack: 8, 4, 3, 2, 1, 0,

>>> pushFront 6 succeeded

quack: 8, 6, 3, 2, 1, 0, -33686019,

>>> rotate(3)

quack: 8, 6, 3, 2, 1, 0, -33686019,

>>> rotate(-4)

quack: 8, 6, 3, 2, 1, 0, -33686019,

--- # of items: 7

>>> popBack succeeded: -858993460

quack: 8, 6, 3, 2, 1, 0,

>>> popBack succeeded: -858993460

quack: 8, 6, 3, 2, 1,

>>> popBack succeeded: -858993460

quack: 8, 6, 3, 2,

>>> popBack succeeded: -858993460

quack: 8, 6, 3,

>>> popBack succeeded: -858993460

quack: 8, 6,

>>> popBack succeeded: -858993460

quack: 8,

>>> popBack succeeded: -858993460

quack: empty
--- # of items: 0
Again, any help is greatly appreciated. Thank you.