Thread: Using a stack

  1. #1
    Unregistered
    Guest

    Using a stack

    A friend and I are trying to lean how to program in C. We are still beginners and don’t know too much about it and we are trying to make a stack. But we have a problem. We have included the code for our program in this message and if you can please help us to make this program work we will be very happy!
    The problem with the program is when we run it and type ab it will say the contents of the stack is b. When we run it and type abcdefg it will say the contents of the stack is f d b. we want it to say ba the first time and gfedcba the second time. We have looked at the code a lot and we don’t know what is wrong so please help us. The code for the program is in this post below. Thankyou

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define STACK_SIZE 50
    #define TRUE 1
    #define FALSE 0
    
    typedef int Bool;
    
    void make_empty(void);
    Bool is_empty(void);
    Bool is_full(void);
    void push(char c);
    char pop(void);
    void stack_overflow(void);
    void stack_underflow(void);
    
    char contents[STACK_SIZE];
    int top = 0;
    
    int main()
    {
      char temp;
    
      printf("Enter some characters: ");
      while (getchar() != '\n') {
        temp = getchar();
        push(temp);
      }
    
      printf("Contents of stack (should be reversed): ");
      while (is_empty() == FALSE) {
        printf("%c ", pop());
      }
      printf("\n");
    
      return 0;
    }
    
    void make_empty(void)
    {
      top = 0;
    }
    
    Bool is_empty(void)
    {
      return top == 0;
    }
    
    Bool is_full(void)
    {
      return top == STACK_SIZE;
    }
    
    void push(char c)
    {
      if (is_full())
        stack_overflow();
      else
        contents[top++] = c;
    }
    
    char pop(void)
    {
      if (is_empty())
        stack_underflow();
      else
        return contents[--top];
    }
    
    void stack_overflow(void)
    {
      printf("Stack Overflow!\n");
      exit(EXIT_FAILURE);
    }
    
    void stack_underflow(void)
    {
      printf("Stack Underflow!\n");
      exit(EXIT_FAILURE);
    }

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Ok, I seemed to fixed the problem change this:
    Code:
       while (getchar() != '\n') {
        temp = getchar();
        push(temp);
      }
    to:

    Code:
     while ((temp=getchar()) != '\n') {
        push(temp);
     }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Unregistered
    Guest
    That works very well! But we don't understand the difference, what is it doing? Doesn't that code do "exactly the same thing" our code does? Why do we need to move temp=getchar() to the while loop test line instead of have it in the while loop body? Thankyou

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    That works very well! But we don't understand the difference, what is it doing? Doesn't that code do "exactly the same thing" our code does? Why do we need to move temp=getchar() to the while loop test line instead of have it in the while loop body? Thankyou
    Nope. Look at your code that doesn't work again. It calls 'getchar' two times. Once in the if check, once in the temp assignment. That's why it skips every other character.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    WOW!!!
    Thankyou very very much... We did not think about that AT ALL! Now we know about it and are a step closer to being good programmers We have one last question about this program. When we compile it we get a compiler warning about pop() which says that pop needs to return something but it ends without a return statement. Does anyone know how we can fix that? Thankyou

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    WOW!!!
    Thankyou very very much... We did not think about that AT ALL! Now we know about it and are a step closer to being good programmers We have one last question about this program. When we compile it we get a compiler warning about pop() which says that pop needs to return something but it ends without a return statement. Does anyone know how we can fix that? Thankyou
    Code:
    char pop(void)
    {
      if (is_empty())
        stack_underflow();
      else
        return contents[--top];
    }
    Yes, see the first "if" statement? If that happens, "stack_underflow()" happens, but nothing returns. You should do something like:

    Code:
    char pop(void)
    {
        if ( !is_empty()  )
            return contents[--top];
        stack_underflow( );
        return 0;
    }
    There are many ways to do it. You could set it so that 'stack_underflow()' returns something and just do:

    char pop( void ){return is_empty() ? stack_underflow() : contents[--top];}

    If 'stack_underflow' just terminates the program, then nothing is actually returned. Keep this in mind:

    If your program is set to return a value, then all "return" paths, including reaching the end of the function, must result in _something_ being returned.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    when working with stacks becareful with buffers.
    Stacks are fun! especially in asm
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM