The program crashes

Pages: 12
Hello!
Can anyone pease tell me what is wrong with this program?
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
#include <iostream>
using namespace std;

enum tToken 
{
    PLUS, MINUS, MUL, DIV, LPAR, RPAR, NUMBER, END, ERROR
};

tToken Token;        
double TokenValue;

char *srcPos;           // Programmposition

tToken searchToken()
// Söker i aktuell strängposition efter nästa Token.
// Här bestäms värdekonstanten och lagras i variabeln TokenValue.

{
    Token = ERROR;
    if (*srcPos==0)
    {
        Token = END;
    } 
	else 
    {
        switch (*srcPos)
        {
            case '(': Token=LPAR;  break;
            case ')': Token=RPAR;  break;
            case '*': Token=MUL;   break;
            case '/': Token=DIV;   break;
            case '+': Token=PLUS;  break;
            case '-': Token=MINUS; break;
        }
		if (*srcPos>='0' && *srcPos<'9') 
        {
            Token=NUMBER;
            TokenValue = 0.0;
        }
        while (*srcPos>='0' && *srcPos<'9') 
        {
            TokenValue *= 10;
            TokenValue += *srcPos-'0';
            srcPos++;
        }
		if (Token != NUMBER)
        {
            srcPos++;
        }
    }
    return Token;
}

tToken Error(char *s)
{
	cout << s << endl;
    return ERROR;
}

double PlusMinus(); // Prototyp

double tal()
// Utvärderar tal och siffror från tal() och PlusMinus()
{
    double Value;
    switch(Token)
    {
        case NUMBER:
            searchToken();
            return TokenValue;
        case MINUS:
            searchToken();
            return -tal();
        case LPAR:
            searchToken();
            Value = PlusMinus();
            if (Token != RPAR)
            {
				return Error(") expected");}
            {
            searchToken();
            return Value;
        case END:
            return 1;
			}
	}
    return Error("primary expected");
}

double MulDiv()
// Utvärderar multiplikation och division från tal()
{
    double Value;
    // Anropa bara den operation som har högsta prioritet
    Value = tal();
    while (Token==MUL || Token==DIV)
    {
        if (Token==MUL)
        {
            searchToken();
            Value *= tal();
        }
        else if (Token==DIV)
        {
          searchToken();
          Value /= tal();
        }
    }
    return Value;
}

double PlusMinus()
// Utvärderar summa och differens från MulDiv()
{
    double Value;
    // Anropa bara den operation som har högsta prioritet
    Value = MulDiv();
    while (Token==PLUS || Token==MINUS)
    {
        if (Token==PLUS)
        {
            searchToken();
            Value += MulDiv();
        }
        else if (Token==MINUS)
        {
            searchToken();
            Value -= MulDiv();
        }
    }
    return Value;
}

double Utvardering(char *s)
{
    srcPos = s;
    searchToken();      // Bestämmer första Token i förväg
    return PlusMinus();
}

int main(int argc, char* argv[])
{
    double Value = Utvardering(argv[1]);
    cout << Value << endl;
    return 0;
}

As soon as I try to run it it crashes.

Please help
closed account (z05DSL3A)
I've only given it a quick look, but I think that you want to change
if (*srcPos==0)
to
if (srcPos==0)
in

1
2
3
4
5
6
7
8
9
10
11
...
tToken searchToken()
// Söker i aktuell strängposition efter nästa Token.
// Här bestäms värdekonstanten och lagras i variabeln TokenValue.

{
    Token = ERROR;
    if (*srcPos==0)
    {
        Token = END;
...
Last edited on
Are you remembering to run it with command-line arguments?
you might want to change this:
1
2
3
4
5
6
7
8
int main(int argc, char* argv[])
{
  if (argc>1) {
    double Value = Utvardering(argv[1]);
    cout << Value << endl;
  }
  return 0;
}

Ok, I've changed it but all I get when I run it is "Press any key to continue...". Something else must be wrong.

Some more help with this please.
closed account (z05DSL3A)
Are you remembering to run it with command-line arguments?
Yes I'm running it with command-line arguments. And I added a cin, but I don't get an answer back. When i write an expression and press enter all I get is "Press any key to continue...". I can't figure out what's wrong.

1
2
3
4
5
6
7
8
9
10
int main(int argc, char* argv[])
{
	cin >> TokenValue;
	if (argc>1)
	{
		double Value = Utvardering(argv[1]);
		cout << Value << endl;
	}
    return 0;
}


Please help.
closed account (z05DSL3A)
I have taken your original posted code, added the one change mentioned in my first post. Compiled it to test.exe. At the command line, entered 'test 2+2' and the program returned 4.

Try
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char* argv[])
{
    if (argc>1)
    {
        double Value = Utvardering(argv[1]);
        cout << Value << endl;
    }
    else
    {
        cout << "Usage: " << argv[0] << " expresion" << endl;
    }
    return 0;
}

Last edited on
All I get now when I run the program with the changes is "Usage: c:\Documents and Settings\Oscar\Skrivbord\C++\Kalk4\Debug\Kalk4.exe expression"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char* argv[])
{
    if (argc>1)
    {
        double Value = Utvardering(argv[1]);
        cout << Value << endl;
    }
    else
    {
        cout << "Usage: " << argv[0] << " expresion" << endl;
    }
    return 0;
} 


Isn't there supposed to be a cin somewhere?
closed account (z05DSL3A)
Okay, that tells me that you are NOT running the program with command-line arguments!

What you need to do is open a command prompt window, change the directory to

c:\Documents and Settings\Oscar\Skrivbord\C++\Kalk4\Debug

then type

Kalk4 2+2

--
"Isn't there supposed to be a cin somewhere?"
Not if your input is via the command line.

What IDE/compiler are you using?
Last edited on
Visual Studio 2008

btw I can't open the command prompt
closed account (z05DSL3A)
In that case you would have to set the command arguments in the projects property page.
It works now, thanks for your help
Another thing, does anyone know how I should do to make the calculator calculate sin, cos and sqrt?
closed account (z05DSL3A)
http://www.cplusplus.com/reference/clibrary/cmath/
I tried to write it into the program but I'm not very good at it.
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
double TrigSqrt()
// Utvärderar trigonometriska ekvationer från PlusMinus()
{
	double Value;
    // Anropa bara den operation som har högsta prioritet
	Value = PlusMinus();
	while (sToken==SIN || sToken==COS || sToken==TAN || sToken==SQRT)
	{
		if (sToken==SIN)
        {
            searchToken();
            Value = (Value+sin(PlusMinus()));
        }
        else if (sToken==COS)
        {
            searchToken();
            Value = (Value+cos(PlusMinus()));
        }
		else if (sToken==TAN)
		{
			searchToken();
			Value = (Value+tan(PlusMinus()));
		}
		else if (sToken==SQRT)
		{
			searchToken();
			Value = (Value+sqrt(PlusMinus()));
		}
	}
	return Value;
}


I added this but I don't think it looks alright, but I don't know how to fix it.

1
2
3
4
case 's': sToken=SIN;   break;
case 'c': sToken=COS;   break;
case 't': sToken=TAN;   break;
case 'v': sToken=SQRT;  break;

I added this to the switch

PLease help, I need this
closed account (z05DSL3A)
Have you added SIN, COS... to your
1
2
3
4
enum tToken 
{
    PLUS, MINUS, MUL, DIV, LPAR, RPAR, NUMBER, END, ERROR
};
Yes but it doesn't work
closed account (z05DSL3A)
Can you post your full code? I can then step through it to evaluate an expression and see what it is doing. I’m having trouble following it at the moment.
Here's the code

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
using namespace std;

double cos (double x );
double sin (double x );
double tan (double x );
double sqrt (double x );

enum tToken 
{
    PLUS, MINUS, MUL, DIV, SQRT, SIN, COS, TAN, LPAR, RPAR, NUMBER, END, ERROR
};

tToken sToken;        
double TokenValue;

char *srcPos;           // Programposition

tToken searchToken()
// Söker i aktuell strängposition efter nästa Token.
// Här bestäms värdekonstanten och lagras i variabeln TokenValue.

{
    sToken = ERROR;
    if (srcPos==0)
    {
        sToken = END;
    } 
	else 
    {
        switch (*srcPos)
        {
            case '(': sToken=LPAR;  break;
            case ')': sToken=RPAR;  break;
            case '*': sToken=MUL;   break;
            case '/': sToken=DIV;   break;
            case '+': sToken=PLUS;  break;
            case '-': sToken=MINUS; break;
			case 's': sToken=SIN;   break;
			case 'c': sToken=COS;   break;
			case 't': sToken=TAN;   break;
			case 'v': sToken=SQRT;  break;
        }
		if (*srcPos>='0' && *srcPos<'9') 
        {
            sToken=NUMBER;
            TokenValue = 0.0;
        }
        while (*srcPos>='0' && *srcPos<'9') 
        {
            TokenValue *= 10;
            TokenValue += *srcPos-'0';
            srcPos++;
        }
		if (sToken != NUMBER)
        {
            srcPos++;
        }
    }
    return sToken;
}

tToken Error(char *k)
{
	cout << k << endl;
    return ERROR;
}

double PlusMinus(); // Prototyp

double tal()
// Utvärderar tal och siffror från tal() och PlusMinus()
{
    double Value;
    switch(sToken)
    {
        case NUMBER:
            searchToken();
            return TokenValue;
        case MINUS:
            searchToken();
            return -tal();
        case LPAR:
            searchToken();
            Value = PlusMinus();
            if (sToken != RPAR)
            {
				return Error("expected");}
            {
            searchToken();
            return Value;
        case END:
            return 1;
			}
	}
    return Error("primary expected");
}

double MulDiv()
// Utvärderar multiplikation och division från tal()
{
    double Value;
    // Anropa bara den operation som har högsta prioritet
    Value = tal();
    while (sToken==MUL || sToken==DIV)
    {
        if (sToken==MUL)
        {
            searchToken();
            Value *= tal();
        }
        else if (sToken==DIV)
        {
          searchToken();
          Value /= tal();
        }
    }
    return Value;
}

double PlusMinus()
// Utvärderar summa och differens från MulDiv()
{
    double Value;
    // Anropa bara den operation som har högsta prioritet
    Value = MulDiv();
    while (sToken==PLUS || sToken==MINUS)
    {
        if (sToken==PLUS)
        {
            searchToken();
            Value += MulDiv();
        }
        else if (sToken==MINUS)
        {
            searchToken();
            Value -= MulDiv();
        }
    }
    return Value;
}

double TrigSqrt()
// Utvärderar trigonometriska ekvationer från PlusMinus()
{
	double Value;
    // Anropa bara den operation som har högsta prioritet
	Value = PlusMinus();
	while (sToken==SIN || sToken==COS || sToken==TAN || sToken==SQRT)
	{
		if (sToken==SIN)
        {
            searchToken();
            Value = (Value+sin(PlusMinus()));
        }
        else if (sToken==COS)
        {
            searchToken();
            Value = (Value+cos(PlusMinus()));
        }
		else if (sToken==TAN)
		{
			searchToken();
			Value = (Value+tan(PlusMinus()));
		}
		else if (sToken==SQRT)
		{
			searchToken();
			Value = (Value+sqrt(PlusMinus()));
		}
	}
	return Value;
}



double Utvardering(char *k)
{
    srcPos = k;
    searchToken();
    return PlusMinus();
}

int main(int argc, char* argv[])
{
    if (argc>1)
    {
        double Value = Utvardering(argv[1]);
        cout << Value << endl;
    }
    else
    {
        cout << "Usage: " << argv[0] << " expresion" << endl;
    }
	system("pause");
    return 0;
}
Pages: 12