I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting errors of all kinds. I have decided to start from scratch. Any suggestions someone can give me would be greatly appreciated!!
Here is the current code:
#include <iostream>
using std::cout;
using std::cin;
#include <cstring>
using std::strcpy;
int pop ( int &popvalue, int stack[], int &top) ;
int push ( int pushvalue, int stack[], int &top, int size ) ;
void printStack ( const int stack [], int top );
void printArray ( const int stack [], int size );
int menu ( ) ;
void main ()
{
const int STACKSIZE = 5 ; // use to limit the size of the stack
int stack [STACKSIZE] = { 0 } ;
int top = -1 ;
int choice ;
int value ;
int result ;
do {
choice = menu () ;
switch ( choice )
{
case 0:
cout << "\n *** End of Program *** \n";
break;
case 1:
cout << "\nEnter a value to push onto the stack: " ;
cin >> value ;
if (! (result = push ( value, stack, top, STACKSIZE ) ) )
cout << "\n *** Stack Overflow\n" ;
break ;
case 2:
if (! (result = pop ( value, stack, top ) ))
cout << "\n *** Stack Underflow\n" ;
else
cout << "\nValue popped from stack is: " << value << "\n\n" ;
break ;
case 3:
printStack ( stack, top );
break;
case 4:
printArray ( stack, STACKSIZE );
break;
default:
cout << "Invalid selection. Please choose again.\n" ;
}
} while ( choice != 0 );
}
int menu ( )
{
int choice;
cout << "\n1. Push a value onto the stack\n"
<< "2. Pop a value from the stack\n"
<< "3. Display the contents of the stack\n"
<< "4. Display the contents of the entire array\n"
<< "0. Exit\n"
<< "Enter your selection: " ;
cin >> choice ;
return choice;
}
int push ( int pushvalue, int stack[], int &top, int size )
{
if ( top == (size - 1) )
return false ;
stack [ ++top ] = pushvalue ;
return true;
}
int pop ( int &popvalue, int stack[], int &top)
{
if ( top == -1 )
return false ;
popvalue = stack [ top-- ] ;
return true;
}
void printStack ( const int stack [], int top )
{
if ( top == -1 )
cout << "Stack is empty, nothing to display\n" ;
else
{
cout << "Contents of stack:\n" ;
for (int i = top; i >= 0; i--)
cout << " " << stack [ i ] << "\n" ;
}
cout << "\nStack Print Completed\n" ;
}
void printArray ( const int stack [], int size )
{
cout << "Contents of array:\n" ;
for (int i = 0; i < size; i++)
cout << "Index " << i << ": " << stack [ i ] << "\n" ;
cout << "\nArray Print Completed\n" ;
}