command line program

My program needs to read a Polish expression from the command line, for example

expr 2 3 4 + * evaluates 2 x (3+4).

When I try to compile this, I get the error

unknown operand +
unknown operand +
....


So clearly something is not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

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


void push(int element);
int pop();


int main(int argc, char **argv)
{
	
	int op2;
	
	
	while (argc > 1)            
	  {
		
		
	    switch(argv[1][0])  {
			  
		  case '1':
		  case '2':
		  case '3':
		  case '4':
		  case '5':
		  case '6':
		  case '7':
		  case '8':
		  case '9':
		  push(atoi(*argv));
		  --argc;
		  argv++;
		  break;
		 
		  case '+':
		  push(pop() + pop());
		  --argc;
		  argv++;
		  break;
		 
		  case '*':
		  push(pop() * pop());
		  --argc;
		  argv++;
		  break;
		  
		  case '-':
		  op2 = pop();
		  push(pop() - op2);
		  --argc;
		  argv++;
		  break;
		  
		  case '/':
		  op2 = pop();
		  if (op2 != 0) {
			  push(pop() / op2);
			  --argc;
		      argv++;
		  }
		  
		  else
			  printf("error: zero divisor\n");
		  break;
		  
		  default:
		  printf("error: unknown operand %s\n", *argv);
		  break;
		  
	      }
		  
	 }
	 
	 printf ("%d\n", pop());
	 return 0;
	  
 }		  
		  
		 
		 
		 
#define STACKSIZE 1000

int next = 0;             /* next free stack position */
int stack[STACKSIZE];

void push(int element)  {
	
	if (next > STACKSIZE)  printf("error: stack full, can't push %d\n", element); 
    else stack[next++] = element; 
	
}


int pop()   
{
	
	if (next == 0) 
	{
	   printf("stack empty\n"); 
	   return 0;
    }
	
	else 
		return stack[--next]; 
}
		
	
  


i tried compiling this..IT WORKED FINE. CAN YOU PLZ EXPLAIN WHAT UR PROGRAM IS ABOUT.?
SOURABH PRAKASH PATI shouting doesn't help.

pacman169 when you show a compiler error, you have to post the full error including the line number. Just because part of the message means nothing to you, doesn't mean it's irrelevant.
It compiled for me, but prints the error on line 68, but that's because I didn't have any arguments to the program: argc and argv weren't set.

If that is not what is happening to you, consider using a debugger.

So we have a runtime error pacman169 :+)
[edit] @PATI [/edit]
It didn't work fine. And I don't mean to scare you off, but if you don't know what OP's program does, how can you help him?

@pacman169
The problem is the odd way you are handling the command-line arguments. You've got two fencepost errors in there with argc/argv, and one with your stack.

Treat argc and argv as if they were constants. Your main program, then, should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main( int argc, char** argv )
{
    int n;
    int op2;

    for (n = 1; n < argc; n++)
    {
        switch (argv[n][0])
        {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                push(atoi(argv[n]));
                break;

            case '+':
                push(pop() + pop());
                break;

            etc
        }
    }
}

Watch your condition on line 90. It should be if (next >= STACKSIZE), since stack[1000] is an invalid position.

Your program is otherwise fairly well-designed. Good job!

I personally would recommend you spend some time trying to fix your indentation (particularly, match up if and else statements) and pick a single style for you use of braces.

Hope this helps.
Last edited on
Btw is my way of handling the command line arguments really odd? I have seen code like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

int
main (int argc, char **argv)
{
  short int ahead = true, hash = true;
  char *xwin = 0;

  while (argc > 1 && ((argv[1][0] == '-') || (argv[1][0] == '+')))
    {
      switch (argv[1][1])
	{
	case 'a':
	  ahead = (argv[1][0] == '-') ? false : true;
	  break;
	case 'h':
	  hash = (argv[1][0] == '-') ? false : true;
	  break;
	case 's':
	  argc--;
	  argv++;
	  if (argc > 1)
	    strcpy (savefile, argv[1]);
	  break;
	case 'l':
	  if (argc > 1)
	    strcpy (listfile, argv[1]);
	  break;

 .....




Not that it would have anything to do with my code, but in general it would be interesting to know why the author did that.



Last edited on
Because the author of that code is a true programmer hack, and either doesn't know how or doesn't care to write good, maintainable code.

It looks old, but is clearly new. (If it really were old, "true" and "false" wouldn't exist except as an ALL-CAPS macro.)

Don't rely on weird, one-off snippets you find. Try to pick and learn from the cleanest, easiest-to-read code you can find.
Topic archived. No new replies allowed.