Debug Assertion Failure

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 50
Reputation: volscolts16 is an unknown quantity at this point 
Solved Threads: 0
volscolts16's Avatar
volscolts16 volscolts16 is offline Offline
Junior Poster in Training

Debug Assertion Failure

 
0
  #1
Sep 22nd, 2008
I have a problem with the new program, it compiles and and it debugs, but when I get to the command lines and enter my function it gives me a debug assertion failure. We are supposed to be finding the sum of the area of rectangles used for close approximation. Any ideas on how I am getting this assertion failure is greatly appreciated!

  1.  
  2. #include <iostream>
  3. #include <stack>
  4. using namespace std;
  5.  
  6.  
  7. enum token_code {REAL_VALUE, VAR_X, OPERATOR};
  8.  
  9. class token
  10.  
  11. {
  12. public:
  13.  
  14. token(token_code code);
  15.  
  16. token();
  17.  
  18. token(const token &t);
  19.  
  20.  
  21. token_code code();
  22.  
  23. float real_value();
  24.  
  25. char op();
  26.  
  27. char var_x();
  28.  
  29. void set_code(token_code code);
  30.  
  31. void set_real_value(float value);
  32.  
  33. void set_op(char op);
  34.  
  35. void set_var_x(char var_x);
  36.  
  37. token& operator = (const token &t);
  38.  
  39. void print();
  40.  
  41. protected:
  42.  
  43. struct token_record
  44.  
  45. {
  46.  
  47. token_code code;
  48.  
  49. union
  50.  
  51. {
  52.  
  53. float real_value;
  54.  
  55. char op;
  56.  
  57. char var_x;
  58.  
  59. };
  60.  
  61. };
  62.  
  63. token_record the_token;
  64.  
  65. };
  66.  
  67.  
  68.  
  69. #include <iostream>
  70. #include "token.h"
  71. using namespace std;
  72.  
  73. token::token()
  74. {
  75. }
  76.  
  77. void token::print()
  78. {
  79. switch (the_token.code)
  80. {
  81. case REAL_VALUE: cout << the_token.real_value;
  82. break;
  83. case VAR_X: cout << the_token.var_x;
  84. break;
  85. case OPERATOR: cout << the_token.op;
  86. break;
  87. default: cout << "Don't know";
  88. }
  89. cout << " ";
  90. }
  91.  
  92. token::token(token_code code)
  93. {
  94. the_token.code = code;
  95. if (code == REAL_VALUE)
  96. the_token.real_value = 0;
  97. }
  98.  
  99. token::token(const token &t)
  100. {
  101. the_token.code = t.the_token.code;
  102. switch (the_token.code)
  103. {
  104. case REAL_VALUE: the_token.real_value = t.the_token.real_value;
  105. break;
  106. case VAR_X: the_token.var_x = t.the_token.var_x;
  107. break;
  108. case OPERATOR: the_token.op = t.the_token.op;
  109. break;
  110. }
  111. }
  112.  
  113. token& token::operator = (const token &t)
  114. {
  115. the_token.code = t.the_token.code;
  116. switch (the_token.code)
  117. {
  118. case REAL_VALUE: the_token.real_value = t.the_token.real_value;
  119. break;
  120. case VAR_X: the_token.var_x = t.the_token.var_x;
  121. break;
  122. case OPERATOR: the_token.op = t.the_token.op;
  123. break;
  124. }
  125. return *this;
  126. }
  127.  
  128. token_code token::code()
  129. {
  130. return the_token.code;
  131. }
  132.  
  133. float token::real_value()
  134. {
  135. return the_token.real_value;
  136. }
  137.  
  138. char token::op()
  139. {
  140. return the_token.op;
  141. }
  142.  
  143. char token::var_x()
  144. {
  145. return the_token.var_x;
  146. }
  147.  
  148. void token::set_code(token_code code)
  149. {
  150. the_token.code = code;
  151. }
  152.  
  153. void token::set_real_value(float value)
  154. {
  155. the_token.real_value = value;
  156. }
  157.  
  158. void token::set_op(char op)
  159. {
  160. the_token.op = op;
  161. }
  162.  
  163. void token::set_var_x(char var_x)
  164. {
  165. the_token.var_x = var_x;
  166. }
  167.  
  168.  
  169.  
  170. #include "token.h"
  171. #include <iostream>
  172. #include <queue>
  173. #include <iomanip>
  174. #include <cmath>
  175.  
  176. using namespace std;
  177.  
  178. #pragma option -Jgx
  179. const char END_TOKEN = '#';
  180.  
  181. void obtain_interval(float &a, float &b, int &number_of_rectangles);
  182.  
  183. int instack_priority(token t);
  184.  
  185. int infix_priority(token t);
  186.  
  187.  
  188. void infix_to_postfix(queue<token> &postfix);
  189.  
  190. float eval_f(queue<token> postfix, float x);
  191.  
  192. void get_infix_token(token &from_infix);
  193.  
  194.  
  195. void integrate(float a, float b, int number_of_rectangles, float &area);
  196.  
  197. float eval(float v1, float v2, char op);
  198.  
  199. bool is_op(char ch);
  200.  
  201.  
  202.  
  203.  
  204. int main()
  205. {
  206.  
  207. float a, b;
  208. float area = 0;
  209.  
  210. int number_of_rectangles;
  211.  
  212. obtain_interval(a, b, number_of_rectangles);
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(3);
  220. while (a < b)
  221.  
  222. {
  223.  
  224. integrate(a, b, number_of_rectangles, area);
  225.  
  226. cout << "Approximation to area is " << setw(10) << area << endl;
  227.  
  228. cout << endl;
  229.  
  230.  
  231. }
  232.  
  233. return 0;
  234. }
  235.  
  236. void obtain_interval(float &a, float &b, int &number_of_rectangles)
  237.  
  238. {
  239.  
  240. cout << "Enter left and right endpoints: (Type left >= right to quit): ";
  241.  
  242. cin >> a >> b;
  243.  
  244.  
  245. if (a < b)
  246.  
  247. {
  248.  
  249. cout << "Enter number of rectangles for computing area: ";
  250.  
  251. cin >> number_of_rectangles;
  252.  
  253.  
  254. }
  255.  
  256. }
  257.  
  258.  
  259.  
  260. void integrate(float a, float b, int number_of_rectangles, float &area)
  261.  
  262. {
  263. cout << " in integrate() " << endl;
  264.  
  265. stack<token> token_stack;
  266.  
  267. queue<token> postfix;
  268.  
  269. int count;
  270.  
  271. float width, x;
  272.  
  273.  
  274.  
  275. width = (b - a) / number_of_rectangles;
  276.  
  277. count = 0;
  278.  
  279. x = a;
  280.  
  281. infix_to_postfix(postfix);
  282.  
  283. cout << "past infix_to_postfix" << endl;
  284.  
  285.  
  286. while (count < number_of_rectangles )
  287.  
  288. {
  289.  
  290. area = area + eval_f(postfix, x + width / 2.0) * width;
  291.  
  292. x = x + width;
  293.  
  294. ++count;
  295.  
  296. }
  297.  
  298.  
  299.  
  300. }
  301.  
  302. void infix_to_postfix(queue<token> &postfix)
  303.  
  304. {
  305.  
  306. cout << " in infix_to_postfix() " << endl;
  307.  
  308. token bottom_stack(OPERATOR);
  309.  
  310. token from_stack, from_infix;
  311.  
  312. stack<token> token_stack;
  313.  
  314. char ch;
  315.  
  316. bottom_stack.set_op(END_TOKEN);
  317.  
  318. token_stack.push(bottom_stack);
  319.  
  320. cout << "Enter function with spaces between tokens, then <ENTER>";
  321.  
  322. cin.get(ch);
  323.  
  324. do
  325.  
  326. {
  327.  
  328. get_infix_token(from_infix);
  329. //
  330. from_infix.print();
  331. //
  332. if ((from_infix.code() == REAL_VALUE) ||
  333.  
  334. (from_infix.code() == VAR_X))
  335.  
  336. {cout << "1" << endl;
  337. postfix.push (from_infix);
  338. }
  339.  
  340. else if (from_infix.op() == ')')
  341.  
  342. {
  343. cout << "2" << endl;
  344. from_stack = token_stack.top();
  345. token_stack.pop();
  346.  
  347. while (from_stack.op() != '(')
  348.  
  349. {
  350. cout << "2.5" << endl;
  351.  
  352. postfix.push (from_stack);
  353.  
  354. from_stack = token_stack.top();
  355. token_stack.pop();
  356.  
  357. }
  358.  
  359. }
  360.  
  361. else if (from_infix.op() == END_TOKEN)
  362.  
  363. while (! token_stack.empty())
  364.  
  365. {
  366. cout << "3" << endl;
  367.  
  368. from_stack = token_stack.top();
  369. token_stack.pop();
  370.  
  371. postfix.push(from_stack);
  372.  
  373. }
  374.  
  375. else
  376.  
  377.  
  378. {
  379. cout << "4" << endl;
  380.  
  381. from_stack = token_stack.top();
  382. token_stack.pop();
  383.  
  384. while (instack_priority(from_stack) >=
  385.  
  386. infix_priority(from_infix))
  387.  
  388. {
  389.  
  390. postfix.push(from_stack);
  391.  
  392. from_stack = token_stack.top();
  393. token_stack.pop();
  394.  
  395. }
  396.  
  397. token_stack.push(from_stack);
  398.  
  399. token_stack.push(from_infix);
  400.  
  401. }
  402.  
  403. } while (! ((from_infix.code() == OPERATOR) &&
  404.  
  405. (from_infix.op() == END_TOKEN)));
  406.  
  407. }
  408.  
  409.  
  410. void get_infix_token(token &from_infix)
  411.  
  412. {
  413.  
  414. const char SPACE = ' ';
  415.  
  416. char ch = ' ';
  417.  
  418. float multiplier = 0.1;
  419.  
  420. bool left_of_decimal = true;
  421.  
  422. int column = 0;
  423.  
  424.  
  425.  
  426. do
  427.  
  428. {
  429.  
  430. ++column;
  431.  
  432. cin.get(ch);
  433.  
  434. if (is_op(ch))
  435.  
  436. {
  437.  
  438. from_infix.set_code(OPERATOR);
  439.  
  440. from_infix.set_op(ch);
  441.  
  442. }
  443.  
  444. else if (isdigit(ch))
  445.  
  446. {
  447.  
  448. if (column == 1)
  449.  
  450. {
  451.  
  452. from_infix.set_code(REAL_VALUE);
  453.  
  454. from_infix.set_real_value(0);
  455.  
  456. }
  457.  
  458. if (left_of_decimal)
  459.  
  460. from_infix.set_real_value(from_infix.real_value() *
  461.  
  462. 10.0 + (ch - '0'));
  463.  
  464. else
  465.  
  466. {
  467.  
  468. from_infix.set_real_value(from_infix.real_value() *
  469.  
  470. multiplier + (ch - '0'));
  471.  
  472. multiplier = multiplier / 10.0;
  473.  
  474. }
  475.  
  476. }
  477.  
  478. else if (ch == '.')
  479.  
  480. {
  481.  
  482. left_of_decimal = false;
  483.  
  484. multiplier = 0.1;
  485.  
  486. }
  487.  
  488. else if (ch == 'X')
  489.  
  490. {
  491.  
  492. from_infix.set_code(VAR_X);
  493.  
  494. from_infix.set_var_x(ch);
  495.  
  496. }
  497.  
  498. } while ((ch != '\n') && (ch != SPACE) && (ch != END_TOKEN));
  499.  
  500. }
  501.  
  502.  
  503. int infix_priority(token t)
  504.  
  505. {
  506.  
  507. int priority = 0;
  508.  
  509.  
  510.  
  511. switch (t.op())
  512.  
  513. {
  514.  
  515. case '^': priority = 3;
  516.  
  517. break;
  518.  
  519. case '*':
  520.  
  521. case '/': priority = 2;
  522.  
  523. break;
  524.  
  525. case '+':
  526.  
  527. case '-': priority = 1;
  528.  
  529. break;
  530.  
  531. case '(': priority = 4;
  532.  
  533. break;
  534.  
  535. case ')':
  536.  
  537. case END_TOKEN: priority = 0;
  538.  
  539. }
  540.  
  541. return priority;
  542.  
  543. }
  544.  
  545.  
  546. int instack_priority(token t)
  547.  
  548. {
  549.  
  550. int priority = 0;
  551.  
  552.  
  553.  
  554. switch (t.op())
  555.  
  556. {
  557.  
  558. case '^' : priority = 3;
  559.  
  560. break;
  561.  
  562. case '*':
  563.  
  564. case '/' : priority = 2;
  565.  
  566. break;
  567.  
  568. case '+':
  569.  
  570. case '-': priority = 1;
  571.  
  572. break;
  573.  
  574. case '(':
  575.  
  576. case END_TOKEN: priority = 0;
  577.  
  578. }
  579.  
  580. return priority;
  581.  
  582. }
  583.  
  584.  
  585. float eval_f(queue<token> postfix, float x)
  586.  
  587. {
  588.  
  589. token result(REAL_VALUE);
  590.  
  591. token t1, t2, t3;
  592.  
  593. stack<token> token_stack;
  594.  
  595.  
  596. t1 = postfix.front();
  597. postfix.pop();
  598.  
  599.  
  600. postfix.push(t1);
  601.  
  602. while (! ((t1.code() == OPERATOR) && (t1.op() == END_TOKEN)))
  603.  
  604. {
  605.  
  606. if (t1.code() != OPERATOR)
  607.  
  608. if (t1.code() == REAL_VALUE)
  609.  
  610. token_stack.push(t1);
  611.  
  612.  
  613. else
  614.  
  615. {
  616.  
  617. t1.set_code(REAL_VALUE);
  618.  
  619. t1.set_real_value(x);
  620.  
  621. token_stack.push(t1);
  622.  
  623. }
  624.  
  625. else
  626.  
  627. {
  628.  
  629. t2 = token_stack.top();
  630. token_stack.pop();
  631.  
  632. t3 = token_stack.top();
  633. token_stack.pop();
  634.  
  635. result.set_real_value(eval(t3.real_value(), t2.real_value(),
  636.  
  637. t1.op()));
  638.  
  639. token_stack.push(result);
  640.  
  641. }
  642.  
  643. t1 = postfix.front();
  644. postfix.pop();
  645.  
  646. postfix.push(t1);
  647.  
  648. }
  649.  
  650. result = token_stack.top();
  651. token_stack.pop();
  652.  
  653. return result.real_value();
  654.  
  655. }
  656.  
  657.  
  658.  
  659.  
  660. float eval(float v1, float v2, char op)
  661.  
  662. {
  663.  
  664. float result = 1;
  665.  
  666.  
  667.  
  668. switch (op)
  669.  
  670. {
  671.  
  672. case '+' : result = v1 + v2;
  673.  
  674. break;
  675.  
  676. case '-': result = v1 - v2;
  677.  
  678. break;
  679.  
  680. case '*': result = v1 * v2;
  681.  
  682. break;
  683.  
  684. case '/': result = v1 / v2;
  685.  
  686. break;
  687.  
  688. case '^': result = pow(v1, v2);
  689.  
  690. break;
  691.  
  692. }
  693.  
  694. return result;
  695.  
  696. }
  697.  
  698.  
  699. bool is_op(char ch)
  700.  
  701. {
  702.  
  703. return (ch == '+') || (ch == '-') || (ch == '*') || (ch == '/') ||
  704.  
  705. (ch == '^') || (ch == '(') || (ch == ')') || (ch == END_TOKEN);
  706.  
  707. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Debug Assertion Failure

 
0
  #2
Sep 22nd, 2008
Well I don't have time to go through all that code, but assertion errors are usually caused by attempting to delete a value that doesn't exist. It doesn't seem like your code uses any DMA though, so it could be internal (included libraries). Try stepping through/into your program (f10/f11) to try and isolate where exactly the error occurs. Then I may be of more help to you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 50
Reputation: volscolts16 is an unknown quantity at this point 
Solved Threads: 0
volscolts16's Avatar
volscolts16 volscolts16 is offline Offline
Junior Poster in Training

Re: Debug Assertion Failure

 
0
  #3
Sep 22nd, 2008
Yes, the majority of the problem seems to occur in this part of the code:
void infix_to_postfix(queue<token> &postfix)
which is located in the main. Hope this helps in some way.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Debug Assertion Failure

 
0
  #4
Sep 22nd, 2008
If you run the code in the debugger, wait for the assert, then use the stack trace to track back to your code, it should help you figure out the problem.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Debug Assertion Failure

 
0
  #5
Sep 22nd, 2008
Hmmm...Well I have no idea what the queue library is. But I sugguest stepping through to that point, then stepping 'in' (f11 in MSDS) to the queue library on the line where the error occurs. You can then step 'through' this code (f10) until you find the exact line within this library that the error occurs. This, coupled with a watch on some key variables, is usually the best way to pinpoint an error.
Last edited by skatamatic; Sep 22nd, 2008 at 4:36 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC