I had another thread before this one, but I felt that it was getting too long.

Current problem: My operator== is not working properly in my program. I have done some things in my function, but I don't think it is "completely right". My program compiles fine and the output is displayed, but whenever I do something like (s1 == s2), it gives the wrong result like "The 2 stacks are NOT equal). Will someone please help? I am stuck. My whole program is listed below:

Current Output:

s1:
40
30
20
10

s2:
50
30
20
10

The 2 stacks are NOT equal

s1:
20
15
10
5

s2:
20
15
10
5

The 2 stacks are NOT equal


s1:
8
6
4
2

s2:
12
9
6
3

The 2 stacks are NOT equal

Targeted Functions:

template<class Type>
bool stackType<Type>::isEqual(const stackType<Type>& otherStack) 
{
bool bRet = false;

if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop)
{

bRet = true;

for (int j = 0; j < stackTop; ++j) 
    {

if (otherStack.list[j] != list[j]) 
 {
// cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
bRet = false;
break;
  }
    }
}
return bRet;
}

template<class Type>
bool stackType<Type>::operator==(const stackType<Type>& otherStack) 
{
return isEqual(otherStack);
}

template<class Type>
bool stackType<Type>::operator!=(const stackType<Type>& otherStack) 
{
return !isEqual(otherStack); //!(*this == otherStack);
}

main.cpp

#include <iostream>
#include <stdlib.h>
#include "stack.h"

using namespace std;

int main() 
{
stackType<int> s1;
stackType<int> s2;


s1.push(10); s1.push(20); s1.push(30); s1.push(40);
s2.push(10); s2.push(20); s2.push(30); s2.push(50);

cout << "s1: "<<endl; s1.printStack(); cout<<endl;

cout << "s2: "<<endl; s2.printStack(); cout<<endl;

if (s1 == s2)
{
cout<<"The 2 stacks are equal"<<endl;
}
if (s1 != s2)
{
cout<<"The 2 stacks are NOT equal"<<endl;
}

cout <<endl;


s1.push(5); s1.push(10); s1.push(15); s1.push(20);
s2.push(5); s2.push(10); s2.push(15); s2.push(20);

cout << "s1: "<<endl; s1.printStack(); cout<<endl;
cout << "s2: "<<endl; s2.printStack(); cout<<endl;

if (s1 == s2) 
{
cout << "The 2 stacks are equal";
} 
if (s1 != s2)
{
cout << "The 2 stacks are NOT equal" << endl;
}

cout<<endl;
cout<<endl;

s1.push(2); s1.push(4); s1.push(6); s1.push(8);
s2.push(3); s2.push(6); s2.push(9); s2.push(12);

cout << "s1: "<<endl; s1.printStack(); cout<<endl;
cout << "s2: "<<endl; s2.printStack(); cout<<endl;

if (s1 == s2) 
{
cout << "The 2 stacks are equal";
} 
else
{
cout << "The 2 stacks are NOT equal" << endl;
}

cout<<endl;
cout<<endl;

system("PAUSE");
return 0;
}

header file:

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack(); 
             bool isFullStack(); 
             void destroyStack(); 
             void printStack(); 
             void push(const Type& newItem);
             Type top(); 
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             bool operator== (const stackType<Type>&);
             bool operator!= (const stackType<Type>&);
             
             
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);
              bool isEqual(const stackType<Type>&);

};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack() 
{
     return(stackTop == 0);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top() 
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop() 
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}



template<class Type>
bool stackType<Type>::isEqual(const stackType<Type>& otherStack) 
{
bool bRet = false;

if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop)
{
bRet = true;
for (int j = 0; j < stackTop; ++j) 
{
if (otherStack.list[j] != list[j]) 
{
// cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
bRet = false;
break;
}
}
}
return bRet;
}

template<class Type>
bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
return isEqual(otherStack);
}

template<class Type>
bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
return !isEqual(otherStack); //!(*this == otherStack);
}


template<class Type>
void stackType<Type>::printStack()
{
     
 while (!isEmptyStack())
 {
       
  cout<<top()<<endl;
  pop();     
       
 }    
     
     
}



#endif

I get all stacks being equal because you call printStack() on both operands and printStack() pops the contents away. Both stacks are always empty. When doing these tests, make sure that you are clearing out the stack at the right time.

Does this mean that my operator== function works fine and I just need to reorganizae my main.cpp?

Does this mean that my operator== function works fine and I just need to reorganizae my main.cpp?

Precisely. I cannot say for sure that your isEqual() function is working as designed without a thorough test, but my eyeball test did not expose any glaring bugs. ;)

I don't understand why it doesn't work because I have tried several different ways and spent several hours on doing the IsEqual, operator==, and operator!= functions. Is there any other hints or ideas on why it is not working? because I am stuck and don't know what to do.

main.cpp

#include <iostream>
#include <stdlib.h>
#include "stack.h"

using namespace std;

int main() 
{
   stackType<int> s1;
   stackType<int> s2;


s1.push(10); s1.push(20); s1.push(30); s1.push(40);
s2.push(10); s2.push(20); s2.push(30); s2.push(50);
cout << "s1: "<<endl; s1.printStack(); cout<<endl;
cout << "s2: "<<endl; s2.printStack(); cout<<endl;

if (s1 == s2)

cout<<"The 2 stacks are  equal"<<endl;
else

cout<<"The 2 stacks are n equal"<<endl;


cout <<endl;


s1.push(5); s1.push(10); s1.push(15); s1.push(20);
s2.push(5); s2.push(10); s2.push(15); s2.push(20);

cout << "s1: "<<endl; s1.printStack(); cout<<endl;
cout << "s2: "<<endl; s2.printStack(); cout<<endl;

if (s1 == s2) 
{
cout << "The 2 stacks are equal";
} 
else
{
cout << "The 2 stacks are NOT equal" << endl;
}

cout<<endl;
cout<<endl;

s1.push(2); s1.push(4); s1.push(6); s1.push(8);
s2.push(3); s2.push(6); s2.push(9); s2.push(12);

cout << "s1: "<<endl; s1.printStack(); cout<<endl;
cout << "s2: "<<endl; s2.printStack(); cout<<endl;

if (s1 == s2) 
{
cout << "The 2 stacks are equal";
} 
else
{
cout << "The 2 stacks are NOT equal" << endl;
}

cout<<endl;
cout<<endl;

system("PAUSE");
return 0;
}

header file:

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack(); 
             bool isFullStack(); 
             void destroyStack(); 
             void printStack(); 
             void push(const Type& newItem);
             Type top(); 
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             bool operator== (const stackType<Type>&);
             bool operator!= (const stackType<Type>&);
             
             
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);
              bool isEqual(const stackType<Type>&);

};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack() 
{
     return(stackTop == 0);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top() 
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop() 
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}



template<class Type>
bool stackType<Type>::isEqual(const stackType<Type>& otherStack) 
{
bool bRet = false;

if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop)
{
bRet = true;
for (int j = 0; j < stackTop; ++j) 
{
if (otherStack.list[j] != list[j]) 
{
// cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
bRet = false;
break;
}
}
}
return bRet;
}

template<class Type>
bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
return isEqual(otherStack);
}

template<class Type>
bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
return !isEqual(otherStack); //!(*this == otherStack);
}


template<class Type>
void stackType<Type>::printStack()
{
     
 while (!isEmptyStack())
 {
       
  cout<<top()<<endl;
  pop();     
       
 }    
     
     
}



#endif

Is there any other hints or ideas on why it is not working? because I am stuck and don't know what to do.

Your printStack() method is wrong because it prints the stack and clears it at the same time. That is problem #1. If you fix that, then your stack objects will not be cleared when they should be for an accurate 3 part test. That is problem #2.

The printStack() method should look more like this if you intend to have it print and not clear. This fixes problem #1:

template<class Type>
void stackType<Type>::printStack()
{
    for (int x = stackTop-1; x >= 0; --x)
    {
        cout << list[x] << '\n';
    }
}

You can manually clear the stack objects, but a more self contained test would be better. For example, you can generalize the test into a function and then call it 3 times. This allows the stack objects to be destroyed and recreated with each function call, which fixes problem #2:

#include <iostream>

void RunTest(int* values1, int sz1,
             int* values2, int sz2)
{
    using std::cout;

    stackType<int> s1;
    stackType<int> s2;

    for (int x = 0; x < sz1; ++x) s1.push(values1[x]);
    for (int x = 0; x < sz2; ++x) s2.push(values2[x]);

    cout << "s1:\n"; s1.printStack(); cout << '\n';
    cout << "s2:\n"; s2.printStack(); cout << '\n';

    if (s1 == s2) cout << "The 2 stacks are equal\n";
    if (s1 != s2) cout << "The 2 stacks are NOT equal\n";

    cout << "\n\n";
}

int main() 
{
    int values1[4] = {10, 20, 30, 40};
    int values2[4] = {10, 20, 30, 50};

    RunTest(values1, 4, values2, 4);

    values1[0] = 5, values1[1] = 10, values1[2] = 15, values1[3] = 20;
    values2[0] = 5, values2[1] = 10, values2[2] = 15, values2[3] = 20;

    RunTest(values1, 4, values2, 4);

    values1[0] = 2, values1[1] = 4, values1[2] = 6, values1[3] = 8;
    values2[0] = 3, values2[1] = 6, values2[2] = 9, values2[3] = 12;

    RunTest(values1, 4, values2, 4);
}

It is important to learn how to troubleshoot this kind of problem, or you will end up wasting hours debugging code that is not broken.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.