By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

urgent help needed for stack of strings

P: 3
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" ;
}
Nov 7 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
you convert your stack to hold strings rather than ints, e.g. push could look like
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. using namespace std;
  3.  
  4. int push( string pushvalue, string stack[], int &top, int size )
  5. {
  6. if ( top == (size - 1) )
  7. return false ;
  8.  
  9. stack [ ++top ] = pushvalue ;
  10. return true;
  11. }
  12.  
Nov 7 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.