TurboC based, Graphics based program with weird error!

This is a discussion on TurboC based, Graphics based program with weird error! within the C++ Programming forums, part of the General Programming Boards category; Hello, The error that it displays is "Error: Expected )", at the 65th line. but that line (Insert function) does ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Bangalore, India, India
    Posts
    3

    TurboC based, Graphics based program with weird error!

    Hello,


    The error that it displays is "Error: Expected )", at the 65th line. but that line (Insert function) does not have any such missing ')'

    I have tried a lot of things, but haven't managed to sort out this error. Please help!

    Code:
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <ctype.h>
    4. #include <dos.h>
    5. #include <string.h>
    6. #include <graphics.h>
    7. struct node
    8. {
    9. int element;
    10. struct node *left;
    11. struct node *right;
    12. int height;
    13. };
    14. typedef struct node *nodeptr;
    15. /*void insert(int nodeptr&);
    16. void del(int nodeptr&);
    17. int deletemin(nodeptr &);
    18. void find(int nodeptr &);
    19. nodeptr findmin(nodeptr);
    20. nodeptr findmax(nodeptr);
    21. void makeempty(nodeptr &);
    22. nodeptr nodecopy(nodeptr &);
    23. void preorder(nodeptr);
    24. void inorder(nodeptr);
    25. void postorder(nodeptr);
    26. int bsheight(nodeptr);
    27. nodeptr singlerotateleft(nodeptr &);
    28. nodeptr doublerotateleft(nodeptr &);
    29. nodeptr singlerotateright(nodeptr &);
    30. nodeptr doublerotateright(nodeptr &);
    31. int max(int,int);
    32. int nonodes(nodeptr);
    33. */
    34. int gdriver=DETECT,gmode=0,errorcode;
    35. char element[3];
    36. int x=1,maxx,midx,xcoord,ycoord,level=320,prevlevel;
    37. void GDisplay(nodeptr p,int xcoord,int ycoord)
    38. {
    39. if (p!=NULL)
    40. {
    41. level=level/2;
    42. setfillstyle(1,BROWN);
    43. setcolor(LIGHTGREEN);
    44. if(p->left->element!=NULL)
    45. line(xcoord,ycoord,xcoord-level,ycoord+50);
    46. if(p->right->element!=NULL)
    47. line(xcoord,ycoord,xcoord+level,ycoord+50);
    48. fillellipse(xcoord,ycoord,10,10);
    49. sprintf(element,"%d",p->element,xcoord,ycoord);
    50. settextstyle(2,0,4);
    51. setcolor(YELLOW);
    52. outtextxy(xcoord-7,ycoord-7,element);
    53. GDisplay(p->left,xcoord-(level),ycoord+50);
    54. GDisplay(p->right,xcoord+(level),ycoord+50);
    55. level=level*2;
    56. }
    57. }//end of GDisplay
    58. void insert(int x,nodeptr& p)
    59. {
    60. if (p == NULL)
    61. {
    62. p = new node;
    63. p->element = x;
    64. p->left=NULL;
    65. p->right = NULL;
    66. p->height=0;
    67. if (p==NULL)
    68. {
    69. gotoxy(4,21);
    70. printf("Out of Space");
    71. }
    72. }
    73. else
    74. {
    75. if (x<p->element)
    76. {
    77. insert(x,p->left);
    78. //GDisplay(root,midx,50);
    79. if ((bsheight(p->left) - bsheight(p->right))==2)
    80. {
    81. if (x < p->left->element)
    82. p = singlerotateleft(p); //single rotation left
    83. else
    84. p = doublerotateleft(p); //double rotation left
    85. }
    86. }
    87. else if (x>p->element)
    88. {
    89. insert(x,p->right);
    90. //GDisplay(root,midx,50);
    91. if ((bsheight(p->right) - bsheight(p->left))==2)
    92. {
    93. if (x > p->right->element)
    94. p = singlerotateright(p); //single rotation right
    95. else
    96. p = doublerotateright(p); //double rotation right
    97. }
    98. }
    99. else
    100. {
    101. gotoxy(4,21);
    102. printf("Element Exists");
    103. }
    104. }
    105. int m,n,d;
    106. m=bsheight(p->left);
    107. n=bsheight(p->right);
    108. d=max(m,n);
    109. p->height = d + 1;
    110. }
    111. nodeptr findmin(nodeptr p)
    112. {
    113. if (p==NULL)
    114. {
    115. gotoxy(4,21); printf("Empty Tree");
    116. return p;
    117. }
    118. else
    119. {
    120. while(p->left !=NULL)
    121. p=p->left;
    122. return p;
    123. }
    124. }
    125. nodeptr findmax(nodeptr p)
    126. {
    127. if (p==NULL)
    128. {
    129. gotoxy(4,21); printf("Empty Tree");
    130. return p;
    131. }
    132. else
    133. {
    134. while(p->right !=NULL)
    135. p=p->right;
    136. return p;
    137. }
    138. }
    139. void find(int x,nodeptr &p)
    140. {
    141. if (p==NULL)
    142. {
    143. gotoxy(4,21);
    144. printf("Element not found");
    145. }
    146. else if (x < p->element)
    147. find(x,p->left);
    148. else if (x>p->element)
    149. find(x,p->right);
    150. else
    151. {
    152. gotoxy(4,21);
    153. printf("Element found !");
    154. }
    155. }
    156. void del(int x,nodeptr &p)
    157. {
    158. nodeptr d;
    159. if (p==NULL)
    160. {
    161. gotoxy(4,21);
    162. printf("Element not found");
    163. }
    164. else if ( x < p->element)
    165. {
    166. del(x,p->left);
    167. if ((bsheight(p->left) - bsheight(p->right))==2)
    168. {
    169. if (x < p->left->element)
    170. p = singlerotateleft(p); //single rotation left
    171. else
    172. p = doublerotateleft(p); //double rotation left
    173. }
    174. }
    175. else if (x > p->element)
    176. {
    177. del(x,p->right);
    178. if ((bsheight(p->right) - bsheight(p->left))==2)
    179. {
    180. if (x > p->right->element)
    181. p = singlerotateright(p); //single rotation right
    182. else
    183. p = doublerotateright(p); //double rotation right
    184. }
    185. }
    186. else if ((p->left == NULL) && (p->right == NULL))
    187. {
    188. d=p;
    189. free(d);
    190. p=NULL;
    191. gotoxy(4,21); printf("Element deleted !");
    192. }
    193. else if (p->left == NULL)
    194. {
    195. d=p;
    196. free(d);
    197. p=p->right;
    198. gotoxy(4,21); printf("Element deleted !");
    199. }
    200. else if (p->right == NULL)
    201. {
    202. d=p;
    203. p=p->left;
    204. free(d);
    205. gotoxy(4,21); printf("Element deleted !");
    206. }
    207. else
    208. p->element = deletemin(p->right);
    209. }
    210. int deletemin(nodeptr &p)
    211. {
    212. int c;
    213. gotoxy(4,21); printf("deltemin");
    214. if (p->left == NULL)
    215. {
    216. c=p->element;
    217. p=p->right;
    218. return c;
    219. }
    220. else
    221. {
    222. c=deletemin(p->left);
    223. return c;
    224. }
    225. }
    226. void preorder(nodeptr p)
    227. {
    228. if (p!=NULL)
    229. {
    230. printf("%d-->",p->element);
    231. preorder(p->left);
    232. preorder(p->right);
    233. }
    234. }
    235. void inorder(nodeptr p)
    236. {
    237. if (p!=NULL)
    238. {
    239. inorder(p->left);
    240. printf("%d-->",p->element);
    241. inorder(p->right);
    242. }
    243. }
    244. void postorder(nodeptr p)
    245. {
    246. if (p!=NULL)
    247. {
    248. postorder(p->left);
    249. postorder(p->right);
    250. printf("%d-->",p->element);
    251. }
    252. }
    253. int max(int value1, int value2)
    254. {
    255. if(value1 > value2)
    256. return value1;
    257. else
    258. return value2;
    259. }
    260. int bsheight(nodeptr p)
    261. {
    262. int t;
    263. if (p == NULL)
    264. return -1;
    265. else
    266. {
    267. t = p->height;
    268. return t;
    269. }
    270. }
    271. nodeptr singlerotateleft(nodeptr &p1)
    272. {
    273. nodeptr p2;
    274. p2 = p1->left;
    275. p1->left = p2->right;
    276. p2->right = p1;
    277. p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
    278. p2->height = max(bsheight(p2->left),p1->height) + 1;
    279. return p2;
    280. }
    281. nodeptr singlerotateright(nodeptr &p1)
    282. {
    283. nodeptr p2;
    284. p2 = p1->right;
    285. p1->right = p2->left;
    286. p2->left = p1;
    287. p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
    288. p2->height = max(p1->height,bsheight(p2->right)) + 1;
    289. return p2;
    290. }
    291. nodeptr doublerotateleft(nodeptr &p1)
    292. {
    293. p1->left = singlerotateright(p1->left);
    294. return singlerotateleft(p1);
    295. }
    296. nodeptr doublerotateright(nodeptr &p1)
    297. {
    298. p1->right = singlerotateleft(p1->right);
    299. return singlerotateright(p1);
    300. }
    301. //int count=0;
    302. int nonodes(nodeptr p)
    303. { int count=0;
    304. if (p!=NULL)
    305. {
    306. nonodes(p->left);
    307. nonodes(p->right);
    308. count++ ;
    309. }
    310. return count;
    311. }
    312. void twolinebox(int x1,int y1,int x2,int y2){
    313. int x,y;
    314. textcolor(WHITE);
    315. gotoxy(x1,y1); cprintf(""); //201
    316. gotoxy(x2,y1); cprintf(""); //187
    317. for(y=y1+1;y<y2;y++){
    318. gotoxy(x1,y); cprintf(""); //186
    319. gotoxy(x2,y); cprintf(""); //186
    320. }
    321. gotoxy(x1,y2); cprintf(""); //200
    322. gotoxy(x2,y2); cprintf(""); //188
    323. for(x=x1+1;x<x2;x++){
    324. gotoxy(x,y1); cprintf(""); //205
    325. gotoxy(x,y2); cprintf(""); //205
    326. }
    327. gotoxy(x1+1,y1+1);
    328. }
    329. void cprintxy(int x,int y,int color,char string[]){
    330. textcolor(color);
    331. gotoxy(x,y); cprintf("%s",string);
    332. }
    333. void center(int y,int color,char string[]){
    334. int x=(80-strlen(string)+1)/2;
    335. textcolor(color);
    336. gotoxy(x,y);cprintf("%s",string);
    337. }
    338. void Temp(void){
    339. int x,y;
    340. clrscr();
    341. twolinebox(1,1,80,24);
    342. for(y=23;y>=1;y--){
    343. sound(60*y);
    344. center(y,YELLOW,"[Adelson-Velskii and Landis Tree]");
    345. gotoxy(2,y+1); clreol();
    346. twolinebox(1,1,80,24);
    347. delay(40);
    348. nosound();
    349. }
    350. center(1,YELLOW,"[Adelson-Velskii and Landis Tree]");
    351. for(x=74;x>=3;x--){
    352. sound(50*x);
    353. cprintxy(x,5,RED,"Press:");clreol();
    354. twolinebox(1,1,80,24);
    355. center(1,YELLOW,"[Adelson-Velskii and Landis Tree]");
    356. delay(20);
    357. nosound();
    358. }
    359. twolinebox(1,1,80,12);
    360. twolinebox(1,1,80,24);
    361. center(1,YELLOW,"[Adelson-Velskii and Landis Tree]");
    362. cprintxy(20,3,GREEN,"[A]- Insertion");
    363. cprintxy(20,4,GREEN,"- Find Minimum");
    364. cprintxy(20,5,GREEN,"[C]- Find Maximum");
    365. cprintxy(20,6,GREEN,"[D]- Search Node");
    366. cprintxy(20,7,GREEN,"[E]- Display Tree");
    367. cprintxy(43,3,GREEN,"[F]- Delete Node");
    368. cprintxy(43,4,GREEN,"[G]- Preorder");
    369. cprintxy(43,5,GREEN,"[H]- Inorder");
    370. cprintxy(43,6,GREEN,"- Postorder");
    371. cprintxy(43,7,GREEN,"[J]- Height");
    372. cprintxy(20,9,GREEN,"[any key]- Quit...");
    373. cprintxy(20,11,LIGHTRED,"Enter your choice: ");
    374. }
    375. void main()
    376. {
    377. nodeptr root,min,max;
    378. int a,findele,delele,leftele,rightele,flag;
    379. char choice,value[2];
    380. char ch='Y';
    381. root = NULL;
    382. textmode(80);
    383. Temp();
    384. do
    385. {
    386. clrscr();
    387. twolinebox(1,1,80,12);
    388. twolinebox(1,1,80,24);
    389. center(1,YELLOW,"[Adelson-Velskii and Landis Tree]");
    390. cprintxy(5,3,LIGHTRED,"Press: ");
    391. cprintxy(20,3,GREEN,"[A]- Insertion");
    392. cprintxy(20,4,GREEN,"- Find Minimum");
    393. cprintxy(20,5,GREEN,"[C]- Find Maximum");
    394. cprintxy(20,6,GREEN,"[D]- Search Node");
    395. cprintxy(20,7,GREEN,"[E]- Display Tree");
    396. cprintxy(43,3,GREEN,"[F]- Delete Node");
    397. cprintxy(43,4,GREEN,"[G]- Preorder");
    398. cprintxy(43,5,GREEN,"[H]- Inorder");
    399. cprintxy(43,6,GREEN,"- Postorder");
    400. cprintxy(43,7,GREEN,"[J]- Height");
    401. cprintxy(20,9,GREEN,"[any key]- Quit...");
    402. cprintxy(20,11,LIGHTRED,"Enter your choice:\>");
    403. choice=getch();
    404. switch(toupper(choice))
    405. {
    406. case 'A':
    407. do{
    408. gotoxy(4,14); printf("Enter node value: ");
    409. a=atoi(gets(value));
    410. if(atoi(value)==0)
    411. {
    412. gotoxy(4,21); printf("Error!!! Enter a valid integer value only.");
    413. gotoxy(4,22); printf("Press any key to continue...");
    414. getch();
    415. }
    416. }while(atoi(value)==0);
    417. insert(a,root);
    418. gotoxy(4,15);
    419. inorder(root);
    420. initgraph(&gdriver,&gmode,"c:\tc\bgi");
    421. errorcode = graphresult();
    422. maxx=getmaxx();
    423. midx=maxx/2,xcoord=midx/2,ycoord=40;
    424. if (errorcode != grOk)
    425. {
    426. printf("Graphics error: %d",grapherrormsg(errorcode));
    427. printf("Press any key to stop");
    428. getch();
    429. exit(1);
    430. }
    431. cleardevice();
    432. GDisplay(root,midx,50);
    433. getch();
    434. restorecrtmode();
    435. break;
    436. case 'B':
    437. if(root!=NULL)
    438. {
    439. min=findmin(root);
    440. gotoxy(4,14); printf("Min element : %d",min->element);
    441. }
    442. break;
    443. case 'C':
    444. if(root!=NULL)
    445. {
    446. max=findmax(root);
    447. gotoxy(4,14); printf("Max element : %d",max->element);
    448. }
    449. break;
    450. case 'D':
    451. gotoxy(4,14); printf("Search node :");
    452. scanf("%d",&findele);
    453. if(root!=NULL)
    454. find(findele,root);
    455. break;
    456. case 'E':
    457. initgraph(&gdriver,&gmode,"c:\tc\bgi");
    458. errorcode=graphresult();
    459. maxx=getmaxx();
    460. midx=maxx/2;
    461. xcoord=midx/2;
    462. ycoord=40;
    463. if(errorcode!=grOk)
    464. {
    465. printf("Graphics error: %d", grapherrormsg(errorcode));
    466. printf("Press any key to stop");
    467. getch();
    468. exit(1);
    469. }
    470. cleardevice();
    471. setbkcolor(LIGHTBLUE);
    472. settextstyle(2,0,5);
    473. outtextxy(20,10,"Adelson-Velskii and Landis Tree");
    474. GDisplay(root,midx,50);
    475. outtextxy(20,440,"Programmed by: Frederick Badion");
    476. outtextxy(20,450,"Polytechnic University of the Philippines");
    477. outtextxy(20,460,"2nd year Bachelor of Science in Computer Science");
    478. outtextxy(320,440,"A partial fulfilment for the subject: ");
    479. outtextxy(320,450,"Design and Analysis of Algorithm");
    480. outtextxy(320,460,"Prof. Ria Sagum -Chairperson BSCS-CCMITPUP-Sta.Mesa");
    481. getch();
    482. restorecrtmode();
    483. break;
    484. case 'F':
    485. gotoxy(4,14);printf("Delete node: ");
    486. scanf("%d",&delele);
    487. del(delele,root);
    488. getch();
    489. initgraph(&gdriver,&gmode,"c:tc\bgi");
    490. errorcode=graphresult();
    491. maxx=getmaxx();
    492. midx=maxx/2,xcoord=midx/2,ycoord=40;
    493. if(errorcode !=grOk)
    494. {
    495. printf("graphics error: %d",grapherror(errorcode));
    496. printf("Press any key to stop");
    497. getch();
    498. exit(1);
    499. }
    500. cleardevice();
    501. setbkcolor(LIGHTBLUE);
    502. settextstyle(2,0,5);
    503. outtexty(20,10,"Adelson-velskii and Landis tree");
    504. GDisplay(root,midx,50);
    505. getch();
    506. restorecrtmode();
    507. break;
    508. case 'G':
    509. gotoxy(4,14);printf("preorder printing.....");
    510. gotoxy(4,15);
    511. preorder(root);
    512. break;
    513. case 'H':
    514. gotoxy(4,14);printf("inorder printing....");
    515. gotoxy(4,15);
    516. inorder(root);
    517. break;
    518. case 'I':
    519. gotoxy(4,14);printf("postorder printing....");
    520. gotoxy(4,15);
    521. postorder(root);
    522. break;
    523. case 'J':
    524. gotoxy(4,14);printf("height and depth:%d",bsheight(root));
    525. gotoxy(4,15);printf("no of nodes:%d",nonodes(root));
    526. break;
    527. }
    528. gotoxy(4,22);printf("do you want to continue(y/n)?");
    529. ch=toupper(getch());
    530. }
    531. while (ch!='N');
    532. }
    Attached Files Attached Files
    Last edited by sidx64; 11-18-2012 at 10:46 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    In most cases Indent_style simply helps a lot.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    I would say the problem line is this:
    Code:
    void insert(int x,nodeptr& p)
    {
    That is actually being caused by:
    Code:
    typedef struct node *nodeptr;
    Hiding a pointer with a typedef is usually not a good idea, even if you call it a ??ptr.



    Jim

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    C Programmer guess, "p = new node;" I am thinking maybe this "p = new node();" needed.
    But, it is just a guess from a C programmer on C++ code.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    See the C code thread here -> Weird Turbo C based, Graphics program error
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my first oop based program
    By dhuan in forum C++ Programming
    Replies: 6
    Last Post: 08-06-2010, 05:39 AM
  2. Help With Based Square Root Program...
    By matt.s in forum C Programming
    Replies: 11
    Last Post: 11-09-2009, 04:19 PM
  3. Web browser based program
    By Lauris in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2007, 04:01 PM
  4. simple vector-based graphics format?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-25-2006, 11:41 AM
  5. Runtime error in Class, string based program.... plz help
    By PotitKing in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2001, 07:15 AM

Tags for this Thread


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