pre mature program termination

I wrote the following code for scientific calc for turbo c++ IDE (code for mouse and graphics was different). However our institute insisted on using bloodshed dev c++ IDE. So I converted entire program to bloodshed dev c++ (the coding for mouse and graphics in the code I am posting here is as supported in dev c++). The graphics work fine, the mouse works as expected but only simple calculation work. Whenever I click on "Tan, Sin, Cos, Log, Pow" buttons, the program terminates...
I am using Win 7 32bit, bloodshed Dev C++ 4.9.9.2 with special graphics.h package that is easily available from google search....

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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
  #define _WIN32_WINNT 0x0500
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<graphics.h>
#include <cstdlib>
#include <iostream>
#include <process.h>
#include <windows.h>
#include<winuser.h>
#define P M_PI/180

class BUTTON
{
public:
	int x,y;
	char *ch;
	int len;
	int brd;
	int t;
public:
	BUTTON(){};
	BUTTON(int a,int b,int c,int d,int e,char *k)
	{x=a;y=b;t=c;len=d;brd=e;ch=k;};
	void box(int,int);
	void text();
	void draw();
	void initialize();

}button[]={  BUTTON (230,200,3,40,35,"7"),
	     BUTTON (280,200,3,40,35,"8"),
	     BUTTON (330,200,3,40,35,"9"),
	     BUTTON (380,200,3,40,35,"OFF"),
	     BUTTON (430,200,3,40,35,"CE"),

	     BUTTON (230,245,3,40,35,"4"),
	     BUTTON (280,245,3,40,35,"5"),
	     BUTTON (330,245,3,40,35,"6"),
	     BUTTON (380,245,3,40,35,"*"),
	     BUTTON (430,245,3,40,35,"/"),

	     BUTTON (230,290,3,40,35,"1"),
	     BUTTON (280,290,3,40,35,"2"),
	     BUTTON (330,290,3,40,35,"3"),
	     BUTTON (380,290,3,40,35,"+"),
	     BUTTON (430,290,3,40,35,"-"),

	     BUTTON (230,335,3,40,35,"0"),
	     BUTTON (280,335,3,40,35,"."),
	     BUTTON (330,335,3,40,35,"^"),
	     BUTTON (380,335,3,40,35,"DEL"),
	     BUTTON (430,335,3,40,35,"="),

	     BUTTON (230,380,3,40,35,"Tan"),
	     BUTTON (280,380,3,40,35,"Sin"),
	     BUTTON (330,380,3,40,35,"Cos"),
	     BUTTON (380,380,3,40,35,"Log"),
	     BUTTON (430,380,3,40,35,"Pow"),};



class WINDOW:public BUTTON
{
public:
	WINDOW(){};
	WINDOW(int a, int b,int d,int e){x=a;y=b;len=d;brd=e;};
	void draw();
}cabinate(220,100,260,350);

class SELECT:public BUTTON
{
private:
	char *num[11];
	char *operat;
	int oncedon;
	float number;
	int top,dot;
public:
	SELECT(){top=-1;dot=0;oncedon=0;number=0.0;};
	int check();
	float convert();
	void result_out(float);
	void reset();
	void take_num(int);
	float calculate();
	void output();
	void task(int);
}choice;

void BUTTON::text()
{
setcolor(15);
settextstyle(3,HORIZ_DIR,1);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(x+20,y+22,ch);
}

float SELECT::convert()
{
float sum=0.0;
int cond=0,t=0;
int c=0;
for(int i=top;i>=0;i--,c++)
{
if((*num[i])!='.' && cond==0)
{
sum=sum+((*num[i])-48)*pow(10,c);
}
else if(cond==0)
{
sum=sum*pow(10,-(c));
cond=1;
i--;
}
if(cond==1 && i>=0)
{
sum=sum+((*num[i])-48)*pow(10,t);
t++;
}
}
return(sum);
}

void SELECT::reset()
{
top=-1;
oncedon=0;
dot=0;
output();
settextstyle(3,HORIZ_DIR,1);
setcolor(15);
setfillstyle(SOLID_FILL,15);
bar(230,140,470,195);
//line(x,150,x,150);
outtextxy(460,180,"00");
}

void SELECT::output()
{
int dx=460,x;
for(x=231;x<=469;x++)
{
        setcolor(15);
        setfillstyle(SOLID_FILL,15);
        bar(230,140,470,195);
        //line(x,150,x,150);
}
for(int i=top;i>=0;i--)
{
settextstyle(3,HORIZ_DIR,1);
setcolor(15);
setfillstyle(SOLID_FILL,15);
bar(230,140,470,195);
//line(x,150,x,150);
outtextxy(dx,180,num[i]);
dx-=10;
}
}

void SELECT::result_out(float numb)
{
int i=0,dx=470;
char c[5];
char res[30];
//for(x=231;x<=469;x++)
//{
setcolor(15);
setfillstyle(SOLID_FILL,15);
bar(230,140,470,195);
//line(x,150,x,150);
//}
sprintf(res,"%.4f",numb);
settextstyle(3,HORIZ_DIR,1);
setcolor(15);
//setfillstyle(SOLID_FILL,15);
//bar(230,140,470,195);
//line(x,150,x,150);
while(i<=strlen(res))
{
sprintf(c,"%c",res[strlen(res)-i]);
outtextxy(dx,180,c);
i++;
dx-=10;
}
}

float SELECT::calculate()
{
if(*operat=='+')
number=number+convert();
else if(*operat=='-')
number=number-convert();
else if(*operat=='*')
number=number*convert();
else if(*operat=='/')
number=number/convert();
else if(*operat=='^')
number=pow(number,0.5);
else if(*operat=='t')
{
if(convert()/90==1)
{
settextstyle(3,HORIZ_DIR,1);
setcolor(15);
setfillstyle(SOLID_FILL,15);
bar(230,140,470,195);
//line(x,150,x,150);
outtextxy(340,132,"UNDEFINED");
}
else
number=sin(convert()*P)/cos(convert()*P);
}
else if(*operat=='s')
number=sin(convert()*P);
else if(*operat=='c')
number=cos(convert()*P);
else if(*operat=='p')
number=pow(number,convert());
else if(*operat=='l')
number=log10(convert());
else if(*operat=='=')
number=number;

return(number);
}

void SELECT::take_num(int tsk)
{
if(top<=10)
{
if(tsk==16 && dot==0)
{
dot=1;
top++;
num[top]=button[tsk].ch;
}
else if(tsk!=16)
{
top++;
num[top]=button[tsk].ch;
}
}
}

void SELECT::task(int tsk)
{
if(tsk==4)
{
reset();
return;
}

if(tsk==18)
{
top--;
output();
}

if(tsk==3){
closegraph();
exit(1);
}

if(tsk==20 || tsk==21 || tsk==22 || tsk== 23)
{
if(tsk==20)
*operat='t';
if(tsk==21)
*operat='s';
if(tsk==22)
*operat='c';
if(tsk==23)
*operat='l';
oncedon=1;
}

if((tsk<=2 && tsk>=0) || tsk==16 || (tsk<=7 && tsk>=5) || (tsk<=12 && tsk>=10) || tsk==15)
{
take_num(tsk);
output();
}

if(tsk==8 || tsk==9  || tsk==13 || tsk==14 || tsk==17 || tsk==24)
{
if(oncedon==0)
{
number=convert();
if(tsk==24)
*operat='p';
else
operat=button[tsk].ch;
top=-1;
dot=0;
oncedon=1;
return;
}
}

if(tsk==19 || tsk==8 || tsk==9  || tsk==13 || tsk==14 || tsk==17 )
{
if(oncedon==1)
{
number=calculate();

if(tsk==19)
operat=operat;
else
operat=button[tsk].ch;

result_out(number);
top=-1;
dot=0;
}
}
}


int SELECT::check()
{
int x1,y1,b=0,i;
int cond=0;
b=GetKeyState(VK_LBUTTON) & 0x8000;

POINT coord;
GetCursorPos(&coord);
x1=coord.x;
y1=coord.y;
setcolor(15);
//setfillstyle(SOLID_FILL,15);
//bar(230,140,470,195);
//line(x,150,x,150);
//m.getmousepos(x1,y1,b);
if(b!=0)
{
for(i=0;i<25;i++)
{
if(x1<(button[i].x+button[i].len+10) && x1>button[i].x && y1<(button[i].y+button[i].brd+20) && y1>button[i].y)
{
while(b!=0 && x1<(button[i].x+button[i].len+10) && x1>button[i].x && y1<(button[i].y+button[i].brd+20) && y1>button[i].y)
{
b=GetKeyState(VK_LBUTTON) & 0x8000;

POINT coord;
GetCursorPos(&coord);
x1=coord.x;
y1=coord.y;
//m.getmousepos(x1,y1,b);
delay(20);
}
cond=1;
break;
}
}
if(cond==1)
{
button[i].draw();
}
return(i);
}
return(100);
}

void WINDOW::draw()
{
cabinate.box(3,3);
//display.box(15,16);
}

void BUTTON::box(int col,int bcol)
{
setcolor(bcol);
setfillstyle(SOLID_FILL,col);
rectangle(x,y,x+len,y+brd);
floodfill(x+len/2,y+brd/2,bcol);
}

void BUTTON::initialize()
{
for(int i=0;i<25;i++)
{
button[i].draw();
}
}

void BUTTON::draw()
{
box(15,15);
text();
}

int main()
{
          initwindow(700,700,"Scientific Calculator");
          cabinate.draw();
          setfillstyle(SOLID_FILL,15);
	      bar(230,140,470,195);
          button[0].initialize();
          while(1)
          {
                  choice.task(choice.check());
          }
return 0;
}


I am unable to figure out the problem. Please help me....
Thanks....
Topic archived. No new replies allowed.