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;
}

I should also add the output for both sets.
int stack output:

In the constructor...
Pushing...5
Pushing...10
Pushing...15
Pushing...20
Pushing...25

Trying to push 70...
The stack is full.

The top value in mystack1 = 25

Popping...25
Popping...20
Popping...15
Popping...10
Popping...5
In the destructor...

output for the Char stack program:

In the constructor...

Trying to push F...

The top value in mystack1 = 

Popping...
Popping...
Popping...
Popping...
Popping...
In the destructor...

In my window, the show up as rectangles. Aside from the sloppiness of my code, how do I get the output to show each char that is popping off of the stack?

The problem is on line 14 of the CharStack driver program. You are assigning integers to char. You might want to change that line to store actual chars instead. The integers were being interpretted as ASCII character codes. Try char A=65; cout << A <<endl; to this in action. Every ASCII character less than 32 is unprintable control characters. This should be a little more visible:

char a='1',b='2',c='3',d='4',e='5',f='6';

Oops. I should have said: Try char A=70; cout << A <<endl; to see this in action. Yes, I changed the number to make it clearer exactly what is going on. This is what I get for writing my reply at midnight.

Okay, I see...so I have temporarily changed it to:

//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 CharStack, size 5, called mystack1
    CharStack mystack1(5);


    char a='1',b='2',c='3',d='4',e='5',f='6';
    cout<<"Pushing...a"<<endl;
    mystack1.push(a);
    cout<<"Pushing...b"<<endl;
    mystack1.push(b);
    cout<<"Pushing...c"<<endl;
    mystack1.push(c);
    cout<<"Pushing...d"<<endl;
    mystack1.push(d);
    cout<<"Pushing...e"<<endl;
    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(char)<<endl;



    }

  return 0;
}

now my output displays that it is pushing the characters, but when displaying the top value via peek, it's showing the number value, as well as while popping. what is causing this? I tried like this:

mystack1.peek(char)<<endl<<endl;

but that of course, didn't work, as well as:

cout<<mystack1.pop(char)<<endl;

If our output should be:

Popping...a
Popping...b
Popping...c
Popping...d
Popping...e
Popping...f

Then you need to be storing a,b,c,d,e and f in your stack. right now you are storing 1,2,3,4 and 5 with char a='1',b='2',c='3',d='4',e='5',f='6';. You can change it to char a='a',b='b',c='c',d='d',e='e',f='f'; to get the desired output. The stack is only going to give you what you put into it.

As a side note cout<<mystack1.pop(char)<<endl; makes no sense and should give you a compiler error. If you are trying to cast to a char then you and use cout<<(char)mystack1.pop()<<endl; or since this is C++ `cout<<static_cast<char>(mystack1.pop())<<endl;

commented: That's what I was looking for. As for "f", it's just an extra value to demonstrate when the stack is full. Thanks so much! +0
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.