Undefined Variables that ARE defined

This is a discussion on Undefined Variables that ARE defined within the C Programming forums, part of the General Programming Boards category; Hi, I am working on a calculator with postfix notation. I have copy/pasted a small section of the main program ...

  1. #1
    Teema
    Guest

    Undefined Variables that ARE defined

    Hi,


    I am working on a calculator with postfix notation. I have copy/pasted a small section of the main program below. Even though I declare variables such as E, L, or pow in a typedef statement, my unix compiler does not recognize these variables as defined. Please help.
    Code:
    /* Code begins */
    
    typedef enum
    {
    PLUS='+', MINUS='-', MULT='*', DIV='/', POW='pow',
    LOG='L', EXP='E', NUM='#', PRINT=';', NL='\n',
    UNKNOWN='?'
    } Token;
    
    
    Token cur_token;
    double number;
    
    
    main()
    {
    
    
        double op2, op1;
        char s[MAXOP];
        int top = 0;
    
    
        while ((cur_token = GetToken(s)) != EOF) {
                 switch (cur_token){
                 case NUMBER: push(atof(s));
                         break;
                 case PLUS: push(pop()+ pop()); break;
                 case MULT: push(pop()* pop()); break;
                 case MINUS: op2 = pop();
                                      push(pop() - op2); break;
                 case DIV: op2 = pop();
                                 if (op2 != 0.0)
                                     push(pop()/op2);
                                 else
                                     printf("error: zero divisor\n");
                                     break;
                case EXP: push(E(pop())); break;
                case POW: op2 =pop();
                                  op1 =pop();
                                  push(pow(op1, op2)); break;
                case LOG: push(L(pop())); break;
                case NL: printf("\t%.8g\n", pop()); break;
                case PRINT: printf("\t%.8g", stack[top-1]);
                                    break;
                default: printf("error: unknown command %s\n", s);
                             break;
                 }
             }  /*end of while statement*/
              return 0;
    }
    
    
    
    
    /* Code ends */

    [code][/code]tagged by Salem

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Good lord, use code tags.
    2) while ((cur_token = GetToken(s)) != EOF) {

    GetToken() is undefined. Used here first.

    3) case NUMBER: push(atof(s));

    atof() is undefined. Used here first.

    4) case PLUS: push(pop()+ pop()); break;

    push() and pop() are undefined. Used here first.

    case EXP: push(E(pop())); break;

    E is not defined. Used here fisrst. No, you have not defined E. Look at the above code. Where is E defined? I didn't think so.

    case LOG: push(L(pop())); break;

    L is not defined. Used here first.

    And so on and so on...

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

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    and as far as I know single quotes are only used for single chars , but you have written POW = 'pow'

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by beege31337
    and as far as I know single quotes are only used for single chars , but you have written POW = 'pow'
    Correct. I didn't even notice that one. Basicly, it looks like they may not understand the use of enum. Yes, I suppose you can make assignments such as:

    enum { BLUE = 'B' }

    Which will set up the decimal value of the character 'B' as an enum BLUE, but beyond that, enums can only ever hold integral values. (This is for the sake of the original poster, I just happen to be quoting you.)

    As such, I'm not sure what their specific intent is. I guess they're trying to get input and use it as a switch statement in an easy way of checking the input value.

    In any case, you're correct, single quotes are for single characters, double quotes are for strings. Enums can never be strings. You can never use string values as direct comparison in a case or switch.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference - however they ARE defined
    By scarecrow in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2008, 06:14 PM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 11:04 AM
  5. Creating a user defined number of variables
    By beanroaster in forum C++ Programming
    Replies: 10
    Last Post: 09-14-2005, 04:53 PM

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