What am I doing wrong, stack?

This is a discussion on What am I doing wrong, stack? within the C Programming forums, part of the General Programming Boards category; Hey all. What am I doing wrong with this program? It won't print my list of numbers at all. #include ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    What am I doing wrong, stack?

    Hey all. What am I doing wrong with this program? It won't print my list of numbers at all.

    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #define MAXSTACK 10

    typedef enum boolean {FALSE, TRUE} Boolean;

    void Error (char *);

    typedef char StackEntry;
    typedef struct stack
    {
    int top;
    StackEntry entry[11];
    }Stack;

    void Print (StackEntry);

    Boolean StackEmpty(Stack * stack);
    Boolean StackFull(Stack * stack);

    void CreateStack(Stack * stack);

    void TraverseStack ( Stack * stack, void(*Visit) ());

    void Pop (StackEntry *item, Stack * stack);
    void Push (StackEntry item, Stack * stack);

    int main(void)
    {

    StackEntry x;
    Stack s1, s2;
    CreateStack(&s1);
    CreateStack(&s2);
    printf("\nEnter data: ");
    while( (x=getchar()) != '\n' && ! StackFull(&s1))
    Push(x, &s1);
    printf("\nThe Stack is: ");
    TraverseStack(&s1, Print);
    while( ! StackEmpty(&s1))
    {
    Pop(&x, &s1);
    Push(x, &s2);
    }
    printf("\nThe reverse stack is: ");
    TraverseStack(&s2, Print);
    return 0;
    }
    Boolean StackFull(Stack * stack)
    {
    return stack->top=MAXSTACK;
    }
    Boolean StackEmpty(Stack * stack)
    {
    return stack->top<=0;
    }
    void CreateStack(Stack * stack)
    {
    stack->top=0;
    }
    void TraverseStack( Stack * stack, void(*Visit) ())
    {
    int counter;
    for(counter=0;counter<stack->top;counter++);
    (*Visit) (stack->entry[counter]);
    }
    void Print(char x)
    {
    printf("%c ",x);
    }
    void Push (StackEntry item, Stack * stack)
    {
    if(StackFull(stack))
    Error("\nStack is Full");
    else
    { stack->entry[stack->top]=item;
    stack->top++;

    }
    }
    void Pop (StackEntry *item, Stack *stack)
    {
    if(StackEmpty(stack))
    Error("\nStack is Empty");
    else
    {
    *item=stack->entry[--stack->top];
    }
    }
    void Error (char *message)
    {
    fprintf(stderr, "\nERROR: &s\n", message);
    exit (1);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    Boolean StackFull(Stack *stack)
    {
    	return(stack->top = MAXSTACK);
    }
    Double equals needed, not single.

    Code:
    In function TraverseStack
    for (counter = 0; counter < stack->top; counter++);
    Additional semi-colon used in error, remove it.

    Have fun
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Originally posted by Hammer
    Code:
    Boolean StackFull(Stack *stack)
    {
    	return(stack->top = MAXSTACK);
    }
    Double equals needed, not single.
    The reason being:

    I can "define" something like so:
    int Number = 3;
    Anytime I use the variable Number in my program it will be equal to the "real number" 3.

    When I do like so:
    if Number == 3
    I am comparing, or testing.
    If whatever is stored in Number is actually 3, then this condition will return true.

    Code:
    #include <stdio.h>
    
    int main()
    {
            int Number = 3;
            printf("The variable Number was set to %d\n", Number);
            if(Number == 3)
            {
                    printf("When Nmber is compared to 3\n");
                    printf("Number == 3, it returns true.");
            }
            else
            {
                    printf("Oops");
            }
            return 0;
    }
    Run the preceding code intact, to see how this works.
    Change the first line, to say, int Number = 4; to see the comparison return false.

    A simple example of comparison versus defining variables.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Linked List Using Hashing and Stack
    By m0ntana in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 07:11 AM
  2. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  3. confused about system stack
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-05-2006, 08:14 AM
  4. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  5. Stringy Sums
    By bumfluff in forum C++ Programming
    Replies: 14
    Last Post: 05-15-2006, 01:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21