So I was given a stack program to convert from int to char. I converted it but the output is...not expected. Wanting someone to point me in the right direction. I will post the header and driver program of the int stack and then the header and driver of the char stack.
Here is the int header file:
//Int Stack header file - this file combines the specification and implementation files.
#ifndef INTSTACK_H
#define INTSTACK_H
#include<iostream>
using namespace std;
class IntStack
{
private:
int *stackArray;
int stackSize;
int top;
int count;
public:
//constructor
IntStack(int); //not written inline
~IntStack() //written inline
{
delete[]stackArray;
cout<<"In the destructor...\n";
}
void push(int);
int pop();
int peek();
bool isFull();
bool isEmpty();
int getCount();
};
//function definitions
//constructor
IntStack::IntStack(int size)
{ count = 0;
stackArray = new int[size];
stackSize = size;
top = -1;
cout<<"In the constructor...\n";
}
//function to push integer onto stack
void IntStack::push(int int1)
{
if (!isFull())
{count++;
top++;
stackArray[top] = int1;
}
else
{
cout<<"The stack is full.\n";
}
}
//function to pop the top value off the stack
int IntStack::pop()
{
int int1;
if(!isEmpty())
{
int1 = stackArray[top];
top--;
count--;
}
else
{
cout<<"Stack is empty.\n";
}
return int1;
}
//function to peek at the top value of the stack
int IntStack::peek()
{ int int1;
if(!isEmpty())
{
int1 = stackArray[top];
}
else
{
cout<<"Stack is empty.\n";
}
return int1;
}
//check to see if stack is full
bool IntStack::isFull()
{
if(top == stackSize-1)
return true;
else
return false;
}
//check to see if stack is empty
bool IntStack::isEmpty()
{
if(top==-1)
return true;
else
return false;
}
int IntStack::getCount()
{
return count;
}
#endif
...and the int driver program:
//IntStackMain.cpp driver program to test the IntStack class.
//Feb 17, 2015
#include "IntStack.h"
#include<iostream>
using namespace std;
int main()
{
//declare an instance of IntStack, size 5, called mystack1
IntStack mystack1(5);
//for loop to populate mystack1 with 5, 10, 15, 20, 25
for(int i = 1; i <= 5; i++)
{ cout<<"Pushing..."<<i*5<<endl;
mystack1.push(i * 5);
}
cout<<"\nTrying to push 70...\n";
mystack1.push(70);
cout<<endl;
cout<<"The top value in mystack1 = "<<mystack1.peek()<<endl<<endl;
//use a while loop to display all the values in the stack as I pop them off
while(!mystack1.isEmpty())
{ cout<<"Popping...";
cout<<mystack1.pop()<<endl;
}
return 0;
}
now, here is the conversion that I have done, which should be really simple and I am sure is, but it isn't working as I think it should.
char header file:
//Int Stack header file - this file combines the specification and implementation files.
#ifndef CHARSTACK_H
#define CHARSTACK_H
#include<iostream>
using namespace std;
class CharStack
{
private:
char *stackArray;
int stackSize;
char top;
int count;
public:
//constructor
CharStack(char); //not written inline
~CharStack() //written inline
{
delete[]stackArray;
cout<<"In the destructor...\n";
}
void push(char);
char pop();
char peek();
bool isFull();
bool isEmpty();
char getCount();
};
//function definitions
//constructor
CharStack::CharStack(char size)
{ count = 0;
stackArray = new char[size];
stackSize = size;
top = -1;
cout<<"In the constructor...\n";
}
//function to push integer onto stack
void CharStack::push(char char1)
{
if (!isFull())
{count++;
top++;
stackArray[top] = char1;
}
else
{
cout<<"The stack is full.\n";
}
}
//function to pop the top value off the stack
char CharStack::pop()
{
char char1;
if(!isEmpty())
{
char1 = stackArray[top];
top--;
count--;
}
else
{
cout<<"Stack is empty.\n";
}
return char1;
}
//function to peek at the top value of the stack
char CharStack::peek()
{ char char1;
if(!isEmpty())
{
char1 = stackArray[top];
}
else
{
cout<<"Stack is empty.\n";
}
return char1;
}
//check to see if stack is full
bool CharStack::isFull()
{
if(top == stackSize-1)
return true;
else
return false;
}
//check to see if stack is empty
bool CharStack::isEmpty()
{
if(top==-1)
return true;
else
return false;
}
char CharStack::getCount()
{
return count;
}
#endif
...and the driver program:
//IntStackMain.cpp driver program to test the IntStack class.
//Feb 17, 2015
#include "CharStack.h"
#include<iostream>
using namespace std;
int main()
{
//declare an instance of IntStack, size 5, called mystack1
CharStack mystack1(5);
//for loop to populate mystack1 with 5, 10, 15, 20, 25
char a=1,b=2,c=3,d=4,e=5,f=6;
/*for(char a = 1; a <= 5; a++)
{ cout<<"Pushing..."<< a<<endl;
mystack1.push(a);
}*/
mystack1.push(a);
mystack1.push(b);
mystack1.push(c);
mystack1.push(d);
mystack1.push(e);
cout<<"\nTrying to push F...\n";
mystack1.push(f);
cout<<endl;
cout<<"The top value in mystack1 = "<<mystack1.peek()<<endl<<endl;
//use a while loop to display all the values in the stack as I pop them off
while(!mystack1.isEmpty())
{ cout<<"Popping...";
cout<<mystack1.pop()<<endl;
}
return 0;
}