Thread: I'm learning, need help with STACK!!

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

    I'm learning, need help with STACK!!

    Here is my Code:

    #include <ctype.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    #define NEW(x) (x * )malloc(sizeof(x))
    #define VALID(x) (( * x) != NULL)
    #define NUM(x) (( * x) -> num)


    typedef ROOT STACK;
    int push_double(STACK **stack, double *data)
    {
    double *ptr;
    if((ptr=NEW(double))!=NULL)
    {
    *ptr = *data;
    return(dbl_insert_data(stack,ptr,0));
    }
    return(-1);

    }



    int pop_double(STACK**stack, double *data)

    {
    if(!VALID(stack))
    return(-1);
    *data = *(double*)((*stack)->head->data);
    free((*stack)->head->data);

    if(dbl_delete_node(stack,(*stack)->head)==NULL)
    return(-1);
    else
    return 0;
    }


    int main(void)
    {
    STACK *stack = NULL;
    double d1,d2,d3;
    char buff[BUFSIZ];

    while(1)
    {
    gets(buff);

    if(buff[0]==NULL)
    continue;

    if(buff[0]=='c')
    {
    delete_list(&stack);
    printf("Calculator Cleared\n");
    continue;
    }

    if(!isdigit(buff[0])&& strlen(buff)==1)
    {

    if(!VALID(&stack) || NUM(&stack)<2)
    {
    printf("Not Enough Arguments\n");
    continue;
    }

    pop_double(&stack,&d1);
    pop_double(&stack,&d2);



    switch(buff[0])
    {

    case '+' : d3=d2+d1;
    printf("%g+%g=%g\n",d2,d1,d3);
    push_double(&stack,&d3);
    break;

    case '-' : d3=d2-d1;
    printf("%g-%g=%g\n",d2,d1,d3);
    push_double(&stack,&d3);
    break;

    case '*' d3=d2*d1;
    printf("%g*%g=%g\n",d2,d1,d3);
    push_double(&stack,&d3);
    break;

    case '/' if (d1==0.0);
    printf("Divide by zero\n");
    else
    d3=d2/d1;
    printf("%g/%g=%g\n",d2,d1,d3);
    push_double(&stack,&d3);
    break;

    }
    }

    else
    {
    d1=atof(buff);
    push_double(&stack,&d1);
    }
    }
    return 0;
    }
    --------------------------------------------------------------------

    Here are my compile errors:

    Compiling...
    RPNC2.cpp
    Y:\C Programming\Lab8&9\RPNC2.cpp(19) : error C2146: syntax error : missing ';' before identifier 'STACK'
    Y:\C Programming\Lab8&9\RPNC2.cpp(19) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    RPNC2.exe - 2 error(s), 0 warning(s)

    --------------------------------------------------------------------

    Can anyone please tell me what's wrong with this and how it can be corrected? I know it's straight outta textbook but I thought it strange that it didn't work.

    Thanks,
    H

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You need to define STACK (or include some header file ???). It it a structure, a class?

    And please use code tags:
    Type: &#91;CODE] then insert your code and type &#91;/CODE]

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. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM