473,395 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

urgent help needed for stack of strings

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
1 4191
horace1
1,510 Expert 1GB
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

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

Similar topics

8
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error for some of my web application users but not others??? Even though the application runs from a central webserver??? Thanks for...
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
4
by: Joe Kasta | last post by:
Here's a tricky question..:) I have a a function in a webform that will draw a checkbox in a given area (note, this is an override of a PaintForeground function, not a drop / drag of a checkbox...
13
by: Gohar | last post by:
Any one please send me the C++ code for arithmatic calculator using linked lists Which performs addition ,subtraction,multiplication,division and modulus(using operator overloading). i need it...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
1
by: shahrossi | last post by:
question: 1. Write a program that copies the contents of one stack into another. A function named copystack receives 2 stacks the source stack and the destination stack. The order of the stacks...
1
by: teddymeu | last post by:
hi guys I posted the other day regarding a solution i needed to design, im new to development an asp,net and built a local post office search tool using asp.net 2. vb in visual studio, using an...
9
by: Tarique | last post by:
Hello all.I am trying to implement a stack which can store either integer or float values. The code is given below: #include<stdio.h> #include<stdlib.h> #include<string.h> #define...
77
by: Hans Schneider | last post by:
1. in the prg bellow what vars are stored on stack, heap, data segment? int i; void main() { int j; int *k = (void *)malloc(1); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.