Thread: Question about stack - push - pop

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    6

    Question about stack - push - pop

    Hi,

    I'm sorry to bother you guys with this but I already used the search button and looked at a lot of threads but I just can't figure it out
    I'm kind'of a newbie

    I need to make a program that reads a text file and displays it backwards, this shouldn't be to difficult but I don't have any info on stacks.
    I have to make it for my evening class, the teacher told us to use stack but we didn't see this in class and have no idea how this works.

    I was hoping someone could help me with this one,...
    The part that opens the textfile and displays it on the screen is ready, now I only have to figure out the stack part.


    Thanks in advance,

    Wietse.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A stack is really very simple, you can implement one quite quickly using an array:
    Code:
    #include <iostream>
    #include <string>
    
    const int stack_size = 100;
    
    int main()
    {
      // Stack stuff
      int stack_loc = 0;
      std::string stack[stack_size];
    
      std::string line;
    
      while ( std::getline ( std::cin, line ) )
        stack[stack_loc++] = line; // Push onto the stack
    
      while ( stack_loc != 0 )
        std::cout<< stack[--stack_loc] <<std::endl; // Pop off the stack
    }
    The basic idea is to only add and remove at the top of the stack (or end of the array in this case), so when you push (add) a series of items onto the stack, you can then pop them all off and they'll be in reverse order.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    thanks for the quick reply.
    now it shows I'm new there are some things I don't get
    what is the std:: for?
    And do I need to set a stack length because I don't know how many characters the textfile will have.

    For the reading of the file I used this code

    Code:
    # include <iostream.h>
    # include <fstream.h>
    # include <stdlib.h>
    
    void main()
    
    
    {
    	fstream bestand;
    	char letter;
    	bestand.open ("c:\\autoexec.bat",ios::in);
    	while(!bestand.eof())
    	{
    		bestand.get(letter);
    		cout<<letter;
    	}
    	bestand.close();
    }
    so instead of getline I could use the char letter.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is the std:: for?
    It's a scope resolution for names declared in the std namespace. It's a better (if less concise) way of saying

    using namespace std;

    However, from the code I've seen from you so far it appears you aren't using the current C++ headers, so namespace resolution shouldn't be a worry, just remove every occurance of std::.

    >And do I need to set a stack length because I don't know how many characters the textfile will have.
    If your stack is implemented as a static array as I used then yes, I imagine you haven't worked with linked data structures yet, so a dynamicly sized stack probably isn't available. Since this is a class project you can just assume a large enough size and leave it at that.

    >For the reading of the file I used this code
    See my reply to your post here.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    I looked at the post and wow
    that's something else, our teacher told us that that that was the best way to read a file (it's his code).

    Yours is smaller and I kinda understand it.

    But I can't find how I can connect the 2 codes (reading and stack)
    if I remove the std:: before std::string stack[stack_size] as you said I get an error "undeclared identifier".

    I feel like I'm waisting your time here, sorry that I bother pro's with newbie questions but I'm trying to learn it
    The help is really appreciated.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if I remove the std:: before std::string stack[stack_size] as you said I get an error "undeclared identifier".
    This may work better for your purposes (ie. be easier to integrate):
    Code:
    #include <iostream.h>
    
    #define stack_size 100
    
    int main()
    {
      // Stack stuff
      int stack_loc = 0;
      char stack[stack_size];
    
      char c;
    
      while ( cin.get ( c ) )
        stack[stack_loc++] = c; // Push onto the stack
    
      while ( stack_loc != 0 )
        cout<< stack[--stack_loc] <<endl; // Pop off the stack
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    6
    Thanks a heap,

    I managed to put them together.
    I learned alot in this half hour.

    Untill the next time (possibly tomorrow )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stacks - push - pop
    By student2005 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 08:15 PM
  2. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM
  3. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  4. Stack Question
    By Drew in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-18-2001, 03:01 AM
  5. Replies: 5
    Last Post: 09-17-2001, 06:18 AM