473,657 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Issue implementing stack/push/pop functionality

2 New Member
Hi, Im having some problems with my program. I'm kinda new to this stuff; only been doing this for about a month. What it's suppose to do is ask the user to input a word, and then print out the word in reverse order. It's basically using the pop/push idea...sorta. I've tried a variety of ways to convert the string to char to get it to print, but all of them havent worked. Any ideas?

Here's where I ended up...

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <cstring.h>
  3.  
  4. const Maxstack = 5;
  5. int Top;
  6.  
  7. //Prototypes
  8. void Push(char Stack[][50], char item);
  9. void Pop(char Stack[][50], char item);
  10. bool Empty();
  11. bool Full();
  12. void Init();
  13.  
  14. main()
  15. {
  16.   char Stack[Maxstack][50];
  17.   //char Item[50];
  18.   char item;
  19.   char ans;
  20.   ans ='y';
  21.   string name;
  22.   string mystack;
  23.   int i = 0;
  24.  
  25. while(ans=='y' || ans == 'Y')
  26. {
  27.  
  28.   cout<<"Enter a word: ";
  29.   cin>>name;
  30.  
  31.   Init();
  32.   for(i=0; i<=name.length(); i++)
  33.   {
  34.    mystack = name.substr(i,1);
  35.    Push(Stack, mystack);
  36.    Pop(Stack, item);
  37.    cout<<item<<endl;
  38.   }
  39.   cout<<"\nDo you want to continue: ";
  40.   cin>>ans;
  41. }
  42.  
  43. return 0;
  44. }
  45.  
  46. void Push(char Stack[][50], char item)
  47. {
  48.     if(Full())
  49.    {
  50.      cout<<"Full";
  51.    }
  52.    else
  53.    {
  54.      strcpy(Stack[++Top], item);
  55.    }
  56. //Stack[++Top] = item;
  57.  
  58. }
  59.  
  60. void Pop(char Stack[][50], char item)
  61. {
  62.   if(Empty())
  63.   {
  64.     cout<<"Empty";
  65.     //strcpy(item, "");
  66.   }
  67.   else
  68.    strcpy(item, Stack[Top]);
  69.    Top--;
  70.   //return StackTop--];
  71. }
  72.  
  73.  
  74.  
  75. bool Empty()
  76. {
  77.   return(Top==-1);
  78. }
  79.  
  80. bool Full()
  81. {
  82.   return (Top+1==Maxstack);
  83. }
  84.  
  85. void Init()
  86. {
  87.   Top =-1;
  88. }
Feb 27 '08 #1
3 1773
manontheedge
175 New Member
there's some stuff in your code that's a bit strange. Can you explain what's going on in that code? I mean, you say you've been doing this for about a month ... do you know what that code is doing?

What I'm getting at is that the basic idea of your question is a much simpler problem, the code you're attempting this with is way too complicated and sparadic. So, the idea is ...

read a word in ... you can put this word in a char array, for example

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. char* input;
  4.  
  5. or
  6.  
  7. char input[50];
  8.  
  9.  
then you need to know how long that input is, one way of doing that is using

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. strlen( input );
  4.  
  5.  
once you know what the length of the string is, you can print it out backwords by using a loop. Something like,

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. for ( i = strlen( input); i > 0; i-- )
  4. {
  5.     cout << input[ i ];
  6. }
  7.  
  8.  

this is just to give you an idea, there are many ways to do this. My point though is to look at the problem at a basic level, and not to over complicate it.
Feb 27 '08 #2
xr0krx
2 New Member
Ah okay, it finally works now. Thanks for the help.
Feb 27 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
You should be using iostream and not iostream.h.

iostream.h is pre-ANS C++ from prior to 1998.

Also, I assume this is homework since a stack template already exists in the STL.
Feb 27 '08 #4

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

Similar topics

354
15788
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets hooked up to interfaces. The Gtk is more than enough gui.
8
2730
by: Ben | last post by:
Hi all, I implemented a stack in C++ in 2 different ways and I'd like to know which approach is better than the other.. and if there is any difference between the two? I'd also like to know if the destructor i'm using is correct.. I got segmentation fault in my second approach while I quit the program. I appreciate any help.... My first appoach goes like this:
2
4015
by: AshifToday | last post by:
Expression Evaluation in Post-Fix Notation Using Stack ===================== /* This program has been made by Ashif Zubair. Roll # 1468, B.Sc.(Hons.) in C.S. ALL RIGHTS ARE RESERVED TO ASHIF ZUBAIR. PROGRAM OBJECTIVE :- */
3
2447
by: Thomas Ibbotson | last post by:
I've been going through a tutorial on C++ and I've come across what I regard as some strange behaviour. As part of the tutorial on classes a simple stack is implemented with functions push() and pop(). I test this stack using: stack a_stack; // Create an instance of stack // Push some data onto the stack a_stack.push(1); a_stack.push(2);
20
3474
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
8
2090
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to ignore spaces, case sensitivity and punctuation. I need three text boxes. The first will be for input of the string. The second will display the string when the "check" button is clicked and the result of the analysis (string is a palindrome or is...
7
7224
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways coming back, I've fallen in love with all the improvements in the language such as the STL. Congrats to anyone who worked on it, you did an excellent job.
11
1773
by: tom | last post by:
Hi! Im new to Python and doing exercise found from internet. It is supposed to evaluate expression given with postfix operator using Stack() class. class Stack: def __init__(self): self.items = def push(self, item): self.items.append(item)
9
2505
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 STACKSIZE 100
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.