0

I am trying to double a stack once it gets full. I have tried calling the copy constructor, and when I call it the stack doesn't keep pushing words. The code run perfectly before it gets full, but once it gets full is where my problem occur. What am I doing wrong?

    #include<iostream>
    #include<fstream>
    #include<string>
    //#include "ArgumentManager.h"
    #include "Stack.h"


    using namespace std;



    int main(int argc, char** argv){

        //ArgumentManager am(argc, argv); //Instantiating parser for command line arguments

        //ReAdInG tHe InPuT fIlE nAmE
        //ifstream infile(am.get("A").c_str()); // file to read from, getting name from command line
        //ofstream outfile(am.get("C").c_str()); // output file, getting name from command line
        ifstream infile;
        ofstream outfile;
        arrayStack<string> firstStack(10);
        arrayStack<string> secondStack(firstStack);
        firstStack.stackInitialize();
        infile.open("input.txt");
        outfile.open("output.txt");

        //iF tHe FiLe IsNt ReAd
        if(!infile){
            std::cout << "ErRor[Input file not Found] YoU hAd OnE jOb....ShAmE" << endl;
            std::cout << "ExItInG PrOgRaM!!! GoOdByE" << endl;
            return 0;


        };

        string tester; // to get the words 

        //READ FROM INFILE AND OUTPUT TO OUTFILE
        while(infile >> tester){

            for(int i = 0; i < tester.size(); ++i)
            { // CHECK IF A SPECIAL CHARACTER IS ON THE FILE 
                if(!((tester[i] >= 'a' && tester[i] <= 'z')||(tester[i] >= 'A' && tester[i]<= 'Z')))
                    {
                    tester[i] = '\0';
                    }
            }
            firstStack.push(tester);
        };

            while(!firstStack.stackIsEmpty())
            { 
                string b = firstStack.top();
                outfile << b << " ";
                cout << b << " ";
                if(firstStack.stackIsFull()){
                secondStack.push(tester)
                };
                firstStack.pop();


            }



        infile.close();
        outfile.close();

        return 0;
    }

Also I have tried to call the copy constructor in the push function such as:

        template <class Type>
    void arrayStack<Type>::push(const Type& word){
        if(topStack != maxStackSize){
            list[topStack] = word; // adding a new word to the STACK
            topStack++;
        }
        else
            cout << "YOU KNOW YOU CAN'T ADD TO A FULL STACK............SHAME" << endl;
            arrayStack<Type> newArrayStack(maxStackSize*2);

            for(int i = 0; i < maxStackSize; i++){
                newArrayStack.push(list[i]);
            }
            newArrayStack.push(word);
            stackCopy(newArrayStack);
    }

It doesn't work either.

Below is the Stack Template

   

        //ARRAY BASED STACK TEMPLATE
#ifndef H_ArrayStack
#define H_ArrayStack

#include <iostream>

using namespace std;

template <class Type>
class arrayStack{
 private:
  int maxStackSize; // the maximum height of the STACK
  int topStack; // the top of the STACK
  void stackCopy(const arrayStack<Type>& newArrayStack);
  Type *list; // array based needs pointer to hold the stack element
 public:
  const arrayStack<Type>& operator=(const arrayStack<Type>&);

  void stackInitialize(){ topStack = 0;}; //Ensure the array stack is empty
  bool stackIsEmpty() const{return(topStack == 0);}; //check if stack is empty, is const so will not be messed with
  bool stackIsFull() const{return topStack == maxStackSize;}; // just like line 8 except check if it is full

  void push(const Type& word); // add a word to the array STACK
  void pop(); //remove a word from the array and increment the top

  Type top() const; //returns the top of the STACK

  arrayStack(int size); //the default constructor
  arrayStack(const arrayStack<Type>& newArrayStack); // the copy constructor which allows a new STACK
  ~arrayStack(){delete [] list;}; // it is an array so to ensure no memory leaks the stack must be deleted after use

};
template <class Type>
void arrayStack<Type>::push(const Type& word){
 if(topStack != maxStackSize){
  list[topStack] = word; // adding a new word to the STACK
  topStack++;
 }
 else{

  cout << "YOU KNOW YOU CAN'T ADD TO A FULL STACK............SHAME" << endl;
  int size = maxStackSize; 
  maxStackSize *= 2;  
  Type *temp = new Type[maxStackSize]; // create temp and double the size of list
  for(int i = 0; i < size; i++){ // copy over all the values
      temp[i] = list[i];
  } 
  delete [] list;       // delete the list 
  list = temp;          // point to the resized list
  list[topStack] = word;
  topStack++;
 }
}
template <class Type>
void arrayStack<Type>::pop(){
 if (!stackIsEmpty()){
  topStack--;
 }
}
template <class Type>
Type arrayStack<Type>::top() const{
 if(topStack == 0){
  return 0;
 }
 else
  return list[topStack - 1];
}
template <class Type>
arrayStack<Type>::arrayStack(int size){
 maxStackSize = size;
 topStack = 0;
 list = new Type[maxStackSize];
}
template <class Type>
void arrayStack<Type>::stackCopy(const arrayStack<Type>& newArrayStack){
 maxStackSize = newArrayStack.maxStackSize;
 topStack = newArrayStack.topStack;
 list = new Type[maxStackSize];
 for(int j = 0; j < topStack; j++)
  list[j] = newArrayStack.list[j];
}
template <class Type> 
arrayStack<Type>::arrayStack(const arrayStack<Type>& newArrayStack){
 stackCopy(newArrayStack);
}
template <class Type>
const arrayStack<Type>& arrayStack<Type>::operator=(const arrayStack<Type>& newArrayStack){
 if(this != &newArrayStack)
  stackCopy(newArrayStack);
 return *this;
}
#endif
halfer
  • 19,824
  • 17
  • 99
  • 186
okeith12
  • 1
  • 2
  • You don't have brackets {} around your else case in `push`. Also, you are setting list to `NULL` in your `arrayStack` copy constructor before you call `stackCopy` which deletes `list`. This is a memory leak. – MFisherKDX Oct 30 '17 at 03:56
  • why don't you make another dynamic array double the size of the original and copy over all the values from the original and delete the original and point original to the new array. This way you don't have to instantiate another arrayStack and call copy constructor and other functions. – Manvir Oct 30 '17 at 04:03
  • @MFisherKDX how would I go about fixing the memory leak and brackets around the original push function or the one where I called the copy constructor – okeith12 Oct 30 '17 at 04:03
  • @Tyger I tried that in my push function when I called a new array doubled the size. It does double, but it doesn't continue pushing, it stops at 10 and keep saying it is full – okeith12 Oct 30 '17 at 04:05
  • can I see how you tried doing that? – Manvir Oct 30 '17 at 04:18
  • @Tyger second code in my post – okeith12 Oct 30 '17 at 04:20
  • @okeith12 [here](http://rextester.com/live/JWJS61701) is a working example – Manvir Oct 30 '17 at 04:43
  • @Tyger, in my main file, if i'm reading from a file of say 20 words, it only reads ten and output only ten.vHow would i fix that in my main – okeith12 Oct 30 '17 at 05:09

1 Answers1

0

From:

arrayStack<Type> newArrayStack(maxStackSize*2);
for(int i = 0; i < maxStackSize; i++){
      newArrayStack.push(list[i]);
}
newArrayStack.push(word);
stackCopy(newArrayStack);

To:

int size = maxStackSize; 
maxStackSize *= 2;  
Type *temp = new Type[maxStackSize]; // create temp and double the size of list
for(int i = 0; i < size; i++){ // copy over all the values
    temp[i] = list[i];
} 
delete [] list;       // delete the list 
list = temp;          // point to the resized list
list[topStack] = word;
topStackk++;

Try this in you push function this will double the size of the stack. Like someone mentioned in the comments you have errors in your copy constructor and stackcopy function

Manvir
  • 769
  • 1
  • 7
  • 15
  • ,.Mind telling how to change the errors in the functions. So i changed my function in push,and it works if i am manually pushing strings; however, yet it doesn't keep pushing when I am taking the strings from a file into the stack. It stops at the original size. In my main file, am I calling a copy incorrectly or are my loops incorrect? – okeith12 Oct 30 '17 at 04:46
  • @okeith12 Im still looking sorry it's taking this long. It should push in all the strings from the file I don't understand why that's not working. – Manvir Oct 30 '17 at 05:13
  • @okeith12 Did you try using cout to see if your code is reading all the values from the file? like cout << tester every time you enter the while loop – Manvir Oct 30 '17 at 05:35
  • yea I did that.All of it gets read, its just the stack that doesnt bite – okeith12 Oct 30 '17 at 10:28
  • @okeith12 you are making a variable called stackCopy put you also have a function called stackCopy – Manvir Oct 30 '17 at 17:20
  • @okeith12 the problem is in the stackCopy function. the `delete [] list` shouldn't be there. – Manvir Oct 30 '17 at 17:32
  • Let me check it out, – okeith12 Oct 30 '17 at 17:33
  • I changed it, yet the problem still persists, is it a problem with my main function and how I am looping?? – okeith12 Oct 30 '17 at 17:35
  • @okeith12 did you remove the delete [] list from the stackCopy function and it works for me after doing that. No i don't think there is anything wrong with the loop – Manvir Oct 30 '17 at 17:38
  • @okeith12 Sorry man im unable to reproduce that error it works on mine and i have the same code. try recompiling and cout to pinpoint the origin of the error. I am sorry – Manvir Oct 30 '17 at 18:08
  • I finally got it. You helped alot, I had to restart ! Question, would it be better to call the stack deconstructor at the end or just close the files out ? – okeith12 Oct 30 '17 at 19:20
  • the destructor gets called on its own. when: https://stackoverflow.com/questions/10081429/when-is-a-c-destructor-called – Manvir Oct 30 '17 at 19:32