Why is the Stack not passing the input form the command line

How can I find where the error is? It prints out nested incorrect, I think that it is not pushing anything into the stack, but the peek function is giving an error. I am really confused as to why vim doesn't highlight any errors and it still compiles.

          #include <stdio.h>
            #include <stdbool.h>
            #include <stdlib.h>

            #define MAXLEN 512


            typedef struct {
                char element[MAXLEN];
                int size;
            } stack;

            stack init (void)
            {
                stack S;
                S.element[0] = 'c';    
                S.size = -1;
                return S;
            }

            bool is_empty( stack S );
            char peek( stack S );
            bool is_full( stack S );
            stack push(stack S, char ch);
            char  pop (stack S);
            void exit_stack_overflow(void);
            void exit_stack_underflow(void);
            void exit_incorrect_nesting(void);


            int main(void)
            {

            char ch;
            stack S;
            char buf[MAXLEN]; // should i use this or have it in the struct
            int i = 0;
            S = init();
            int length = sizeof(buf)/sizeof(buf[0]);
                printf("Enter parentheses and/or braces: ");


                fgets(buf, length, stdin); //reads an entire line into stack, no more chars than length
                while (1) {
                    if(buf[i] == '\n'){
                        break;
                    }




                    ch = buf[i];
                    i++;


                    if(ch == '"'){
                       push(S, ch); 
                        if(peek(S) != '\''){

                            if (peek(S) == '"') {
                                pop(S);

                            }
                            else{
                                push(S, ch);


                            }
                        }


                    }
                    else if(ch == '\''){
                        if(peek(S) != '"'){
                            if (peek(S) == '\'') {
                                pop(S);
                            }
                            else
                                push(S, ch);
                        }
                    }
                    else if(peek(S) != '\'' && peek(S) != '"') {
                        switch(ch) {
                            case '(':
                            case '{':

                                push(S, ch);

                                break;
                            case ')':

                                if (is_empty(S) || pop(S) != '(')
                                    exit_incorrect_nesting();
                                break;

                            case '}' :
                                if (is_empty(S) || pop(S) != '{'){

                                    exit_incorrect_nesting();

                                }
                                break;
                        }
                    }
                }


                if (is_empty(S))
                    printf("Nesting is correct\n");
                else {

                    exit_incorrect_nesting();

                }

                return 0;
            }


            bool is_empty( stack S )
            {
                return (S.size == 0);
            }

            bool is_full( stack S )
            {
                return (S.size == MAXLEN - 1);
            }

            char peek( stack S )
            {

                return S.element[S.size];
            }

            stack push( stack S , char ch )
            {
                if (is_full(S)) {
                    fprintf(stderr, "push: Full stack\n");
                    return S;
                }
                ++S.size;
                S.element[S.size] = ch;
                return S;
            }

            char pop( stack S )
            {
                 int i = S.size;
                if(is_empty(S)) {
                    fprintf(stderr, "pop: Empty stack\n");
                    return S.element[i];
                }
                --S.size;
                return S.element[i];
            }

            void exit_stack_underflow(void)
            {
                fprintf(stderr, "Stack Underflow\n");
                exit(EXIT_FAILURE);
            }
            void exit_stack_overflow(void)
            {
                fprintf(stderr, "Stack Overflow\n");
                exit(EXIT_FAILURE);
            }

            void exit_incorrect_nesting()
            {
                printf("Nesting is NOT correct\n");
                exit(EXIT_SUCCESS);
            }

Upvotes: 0

Views: 48

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409482

A major problem is that you pass the stack structure by value to all functions, and that means that the structure is copied and all changes done inside the functions are made on the copies and not the original.

C doesn't support passing by reference, but it can be emulated using pointers.

So for example, the push could be modified like this:

void push( stack * S , char ch )
{
    if (is_full(S)) {
        fprintf(stderr, "push: Full stack\n");
        return;
    }
    ++S->size;
    S->element[S->size] = ch;
}

And called like e.g.

push(&S, ch);

All functions need similar modifications.

Upvotes: 1

Related Questions