my program exit if I enter VAR++ or VAR --, help needed

Hi,

I have problem with my program, if I enter this expression for example
x = 1 + 1
then I enter
x++

the program exit. I don't want it to exit, I want to add 1 to x instead.

could somebody please help me with it


here is a small documentation for this program


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   Language is L = {xa=2+3*4, xb=2*xa+10, ...}
   
   Alphabet is: {var, nr, +, -, *}
   
   Grammar is: Asg -> VAR=E / E
               E -> T+E / T-E / T
               T -> A*T / A
               A -> NR / VAR
               
   The starting letter is Asg
   
   Input: xa = 2+3*4     
          xb = xa + 10
          xc = xa+20
          
   Output: 32
           42
           74
*/


here is the main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Interpreter.h"

int main()
{
    char *p1 = new char(100);
    double ans;
    Interpreter interp1;
    
  
  do
  { 
    interp1.p = p1;
    
    cout<<"Enter an expression: ";
    cin.getline (interp1.p, 100);
    
    interp1.Asg(&ans);
    cout<<ans<<endl;
  
  }while (*p1);
    
    cout<<endl<<endl;
    system("pause");
}



here is the file Interpreter.cpp

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Interpreter.cpp

#include "Interpreter.h"

Interpreter::Interpreter()
{
    token_type = 0;
}

Interpreter::~Interpreter()
{

}

void Interpreter::Asg(double *ans)
{
   string var1;
   Variable oVar1;
   
   get_token();
   
   var1 = token;
   
   if (token_type == 'V')
   {

      cout<<"\nThe variable is: "<<token<<endl;
      
      get_token();
      
      if (*token != '=')
      {
         cout<<"Error: incorrect input"<<endl;
         exit(1);
      }
      
      E(ans);
      
      oVar1.setVarName(var1);
      oVar1.setVarValue(*ans);
      
      pVarLklist.insert (oVar1);
      pVarLklist.print();
      cout<<endl;
   }

   E(ans);
}

void Interpreter::E(double *ans)
{
   char op;
   double temp;
   
   get_token();
   
   T(ans);
   
   while ((op=*token)=='+' || op=='-')
   {
       get_token();
       
       T(&temp);
       
       if (op=='+')
          *ans = *ans + temp;
          
       else if (op=='-')
          *ans = *ans - temp;
          
    }
}

void Interpreter::T (double *ans)
{
     char op;
     double temp;
   
   A(ans);
   
   while ((op=*token)=='*' || op=='/')
   {
       get_token();
       
       A(&temp);
           
       if (op=='*')
          *ans = *ans * temp;
       else if (op=='/')
            *ans = *ans / temp;
    }     
}

void Interpreter::A(double *ans)
{
   if (token_type == 'N')
   {
      *ans = atoi (token);  
      get_token();
      return;
   }

   else if (token_type == 'V')
   {
      *ans = pVarLklist.search (token);  
      get_token();
      return;
   }   
}
void Interpreter::get_token()
{
    char DEL='D', NR='N', VAR='V';
    char *tmp;
    
   token_type = 0;
   tmp=token;
   
   *tmp='\0';
   
   if (!*p) return;
   
   while (isspace(*p)) ++p;
   
   if (strchr ("+-*/()=", *p))
   {
      token_type = DEL;
      *tmp++=*p++;
   }
   
   else if (isdigit(*p))
   {
      while (!isDelim(*p))
      {
         *tmp++=*p++;
      }
      
      token_type = NR;
   }

   else if (isalpha(*p))
   {
      while (!isDelim(*p))
      {
         *tmp++=*p++;
      }
      
      token_type = VAR;
   }
   
   *tmp='\0';
}

int Interpreter::isDelim(char ch) 
{   
   if ((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='(')||(ch==')')||(ch=='=')||(ch=='\r')||(ch==0))
       return 1;
   else 
       return 0;
}   


there are many files in this program, but I believe this is what needed for this problem.

if you need any file I would be more than welcome to add it.
then I enter
x++

the program exit.

So which line is causing the problem? I only see the ++ operator in terms of pointer arithmetic.

Also, this:

char *p1 = new char(100);

does not give you 100 chars. It allocates one char with the value 100. You need to use square brackets around the 100 instead.
There is no errors in the code. as I explained if I enter x++, it crashes.

I upload the whole program in this link, if you want to test it out

http://www.eddieative.com/Interpreter.zip

I see what you mean now by "entering x++". However, the biggest problem I've noticed is the one I mentioned above. Did you try doing this instead in Interp_var_lklist_main.cpp?

char *p1 = new char[100];
Topic archived. No new replies allowed.