I am currently working with dynamic arrays without the use of vector. The program completes different actions on the array class object through a menu. All of the actions seem to work except when testing for equivalency after I copy the first dynamic array (stack1) to a second dynamic array (stack2) of the same capacity. In this scenario, it will state that the two stacks are equivalent even though they are not. For example, the console will output the following when stack 1 is empty and stack 2 contains elements:
I am unsure if this is a problem with my equality operator or assignment operator. My .h file, .cpp file, and main driver file are included below.
h file:
#include <iostream>
class DoubleStack
{
public:
DoubleStack(size_t capacity); //Constructor
DoubleStack(const DoubleStack& rhs); //Copy Cosntructor
~DoubleStack(void); //Destructor
DoubleStack& operator=(DoubleStack& rhs); //Assignment operator overload
int push(double& item); //Add double to top of stack
int pop(double& item); //Remove top double from stack
int empty(void); //Check to see if stack is empty
size_t capacity(void); //Check capacity of stack
size_t size(void); //Check number of elements on stack
int operator==(DoubleStack& rhs); //Comparison operator for two stacks
friend std::ostream& operator<< (std::ostream &output, const DoubleStack& s); //Operator overload for ostream <<
private:
double *data; //pointer for dynamic stack
size_t stack_size; //Capacity of the stack
size_t tos; //Top of stack. 0 == empty stack
size_t noi; //Number of elements on stack
.cpp file:
#include <iostream>
#include "dblstk.h"
//Constructor & Copy Constructor
DoubleStack::DoubleStack(size_t capacity)
{
data = new double[capacity]();
stack_size = capacity;
tos = 0;
noi = 0;
}
DoubleStack::DoubleStack(const DoubleStack& rhs)
{
data = rhs.data;
stack_size = rhs.stack_size;
tos = rhs.tos;
noi = rhs.noi;
}
//Destructor
DoubleStack::~DoubleStack(void)
{
delete[] data;
}
//Overload for comparison operator
int DoubleStack::operator==( DoubleStack& rhs)
{
for (int i = 0; i < tos; i )
{
if (data[i] == rhs.data[i])
{
return true;
}
else
{
return false;
}
}
}
//Assignment operator overload
DoubleStack& DoubleStack::operator=(DoubleStack& rhs)
{
delete[] data;
data = new double[rhs.stack_size];
stack_size = rhs.stack_size;
tos = rhs.tos;
for (int i = 0; i < tos; i )
{
data[i] = rhs.data[i];
};
return *this;
}
//ostream operator overload
std::ostream& operator<< (std::ostream &output, const DoubleStack& s)
{
for (int i = 0; i<s.tos; i )
{
if (i > 0)
{
output << ", ";
}
output << s.data[i];
};
return output;
}
//DoubleStack functions
DoubleStack::push(double& item)
{
if (noi >= stack_size)
{
std::cout<<"Stack size at capacity. Cannot push."<<std::endl;
return 0;
}
else
{
noi ;
data[tos ] = item;
std::cout<<"Inserted "<<item<<std::endl;
return 1;
}
}
DoubleStack::pop(double& item)
{
if (DoubleStack::empty() == true)
{
std::cout<<"Cannot remove number because stack is empty."<<std::endl;
return 0;
}
else
{
noi--;
data[tos] = item;
data[tos--];
std::cout<<"Removed "<<data[tos]<<std::endl;
return item;
return 1;
}
}
DoubleStack::empty(void)
{
if (noi == 0)
{
return true;
}
else
{
return false;
}
}
size_t DoubleStack::capacity(void)
{
return stack_size;
}
size_t DoubleStack::size(void)
{
return noi;
}
main file:
#include <iostream>
#include "dblstk.h"
#include "stack_driver.cpp"
//Menu function
void Menu()
{
std::cout<<"-----------------------------------------------------"<<std::endl;
std::cout<<"\t \t Welcome to the Stackhouse. \t \t"<<std::endl;
std::cout<<"-----------------------------------------------------"<<std::endl;
std::cout<<"1. Display Stack 1"<<std::endl;
std::cout<<"2. Place a double value onto the top of stack 1"<<std::endl;
std::cout<<"3. Remove a value from the top of stack 1"<<std::endl;
std::cout<<"4. Check the total capacity of stack 1"<<std::endl;
std::cout<<"5. Check current number of items on stack 1"<<std::endl;
std::cout<<"6. Copy stack 1 to stack 2"<<std::endl;
std::cout<<"7. Check to see if the two stacks are equal"<<std::endl;
std::cout<<"8. Quit"<<std::endl;
std::cout<<"Enter menu selection: "<<std::endl;
}
int main(void)
{
DoubleStack stack1(10);
DoubleStack stack2(10);
Menu();
int user_input;
std::string answer;
do
{
std::cout<<"Select an option (1-8)"<<std::endl;
std::cin>>user_input;
switch (user_input)
{
case 1:
{
std::cout<<"Displaying stack 1:"<<std::endl;
std::cout<<stack1<<std::endl;
break;
}
case 2:
{
std::cout<<"Enter number to push: "<<std::endl;
double push_item;
std::cin>>push_item;
stack1.push(push_item);
break;
}
case 3:
{
std::cout<<"Popped top number from stack."<<std::endl;
double pop_item;
stack1.pop(pop_item);
break;
}
case 4:
{
std::cout<<"The capacity of stack 1 is: "<<stack1.capacity()<<std::endl;
break;
}
case 5:
{
std::cout<<"The current number of items on stack 1 is: "<<stack1.size()<<std::endl;
break;
}
case 6:
{
std::cout<<"Copying stack 1 to stack 2..."<<std::endl;
stack2 = stack1;
std::cout<<"Stack 1 is: "<<stack1<<std::endl;
std::cout<<"Stack 2 is: "<<stack2<<std::endl;
break;
}
case 7:
{
std::cout<<"Checking to see if the two stacks are equal.."<<std::endl;
if (stack1 == stack2)
{
std::cout<<"The stacks are the same!"<<std::endl;
std::cout<<"Stack 1 is: "<<stack1<<std::endl;
std::cout<<"Stack 2 is: "<<stack2<<std::endl;
}
else
{
std::cout<<"The stacks are not the same!"<<std::endl;
std::cout<<"Stack 1 is: "<<stack1<<std::endl;
std::cout<<"Stack 2 is: "<<stack2<<std::endl;
}
break;
}
case 8:
{
std::cout<<"Leaving the Stackhouse. Quitting..."<<std::endl;
return 0;
break;
}
default:
{
std::cout<<"Inavlid."<<std::endl;
break;
}
}
std::cout<<"Press either F/f to continue"<<std::endl;
std::cin>>answer;
}
while (answer == "F" || answer == "f");
}
CodePudding user response:
Your equality operator is bugged. Think about what equality means. Two array are equal if all the items are equal, but they are not equal if any of the items are not equal. But that's not what the code for you equality operator says because you decide whether they are equal or not equal after looking at the first item only.
//Overload for comparison operator
int DoubleStack::operator==( DoubleStack& rhs)
{
for (int i = 0; i < tos; i )
{
if (data[i] == rhs.data[i])
{
return true;
}
else
{
return false;
}
}
}
Here's a partly fixed version
//Overload for comparison operator
int DoubleStack::operator==( DoubleStack& rhs)
{
for (int i = 0; i < tos; i )
{
if (data[i] != rhs.data[i])
{
return false;
}
}
return true;
}
See the difference. As soon as I find one not equal number I return false, but I don't return true until I've checked all the numbers.
There is one other fix needed. Two arrays are never equal if their sizes are different. Here's version that checks for that as well.
//Overload for comparison operator
int DoubleStack::operator==( DoubleStack& rhs)
{
// check if same size
if (size() != rhs.size())
return false;
for (int i = 0; i < tos; i )
{
if (data[i] != rhs.data[i])
{
return false;
}
}
return true;
}