473,405 Members | 2,444 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,405 software developers and data experts.

Issue implementing stack/push/pop functionality

2
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 1754
manontheedge
175 100+
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
Ah okay, it finally works now. Thanks for the help.
Feb 27 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
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
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...
8
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...
2
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...
3
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...
20
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...
8
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...
7
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...
11
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.