943,600 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 559
  • C++ RSS
Apr 13th, 2009
0

c++ problem need help pls

Expand Post »
this is my program
C++ Syntax (Toggle Plain Text)
  1. //
  2. // custorderscf2.cpp
  3. //
  4. #include <cstdlib> // student ...... Jones, Patricia
  5. #include <iostream> // section ...... MW row .... 4
  6. #include <fstream> // due date ..... xx/xx/xx
  7. #include <iomanip> // refer to ..... Gaddis Ch3
  8.  
  9. void SetPrecision(int); // procedure prototype
  10. using namespace std; // avoid std::cin >> ...
  11.  
  12. /* declare global data elements */
  13. int ordnum; // input variables
  14. string partnum; //
  15. int quantity; //
  16. double price; //
  17.  
  18. void readinput (void); //
  19. ifstream infile; // declare file object
  20.  
  21. int main (void) // main function header
  22. { // begin block
  23.  
  24. int oldordnum; // old order number
  25. double de1extprice; // detail line ext price
  26. double cf1extprice; // order total
  27. double cf2extprice; // report total
  28.  
  29. /* begin procedural code */
  30. SetPrecision(2); // set floating points
  31. cf2extprice = 0; // clear report total
  32. infile.open("custorders.bdl"); // open input file
  33.  
  34. /* test for successful open */
  35. if(!infile)
  36. {
  37. cout << "Input file open error!";
  38. exit(-1); // error pgm stop
  39. }
  40.  
  41. /* print column headers */
  42. cout << setw(10) << "ORD NUM"
  43. << setw(10) << "PARTNUM"
  44. << setw(10) << "QUANTITY"
  45. << setw(10) << "PRICE"
  46. << setw(12) << "EXT PRICE" << "\n\n";
  47.  
  48. /* begin processing loop */
  49. readinput(); // read first record
  50. oldordnum = ordnum; // save the control field
  51. while(ordnum != 99999); // test for more data
  52. {
  53.  
  54. cf1extprice = 0; // clear order total
  55. while(ordnum == oldordnum) // same group ?
  56. {
  57.  
  58. cout << setw(10) << ordnum // detail calc
  59. << setw(10) << partnum // level one calc
  60. << setw(10) << quantity // print detail
  61. << setw(10) << price;
  62. de1extprice = quantity * price; //
  63. cout << setw(12) << de1extprice << endl;
  64. //
  65. cf1extprice = cf1extprice + de1extprice; //
  66. //
  67. readinput(); // read next record
  68. }
  69. cout << endl;
  70. cout << setw(52) << cf1extprice << "\n\n"; // order total
  71. cf2extprice = cf2extprice + cf1extprice; // level two calc
  72. oldordnum = ordnum; // save the control field
  73. } //
  74. cout << endl;
  75. cout << setw(52) << cf2extprice; // print report total
  76.  
  77. /* close file and stop run */
  78. cout << endl;
  79. infile.close(); // close file
  80. return(0); // int return to o/s
  81. } // end main block
  82.  
  83. void readinput (void)
  84. {
  85. infile >> ordnum >> partnum >> quantity >> price;
  86. if (infile.eof())
  87. ordnum = 99999;
  88. return;
  89. }
  90.  
  91. void SetPrecision(int pDecPlaces) // procedure header
  92. {
  93. cout.setf(ios::right); // right-justified
  94. cout.setf(ios::fixed);
  95. cout.setf(ios::showpoint);
  96. cout.precision(pDecPlaces);
  97. return; // return to caller
  98. }
and the text is
C++ Syntax (Toggle Plain Text)
  1. 21608 AT94 11 21.95
  2. 21608 KL62 2 329.95
  3. 21610 DR93 1 495.00
  4. 21610 DW11 1 399.99
  5. 21613 KL62 4 329.95
  6. 21613 AT94 2 21.95
  7. 21613 FD29 1 159.95
  8. 21614 AT94 10 21.95
  9. 21614 KT03 2 595.00
  10. 21617 BV06 2 794.95
  11. 21617 CD51 4 150.00
  12. 21619 DR93 3 495.00
  13. 21619 KV29 2 1290.00
i dont know y it runs into infinite loop
i guess the problem is on while (ordnum != 99999)
or readinput ()
and i want to keep using while loop and
if (infile.eof())
Last edited by Narue; Apr 13th, 2009 at 4:14 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wuzj1988 is offline Offline
2 posts
since Apr 2009
Apr 13th, 2009
0

Re: c++ problem need help pls

Look at your while loop: you closed it before it started: while(ordnum != 99999); .
Just remove the semicolon right after the while(ordnum != 99999) and everything should be fine
Last edited by unbeatable0; Apr 13th, 2009 at 4:53 pm.
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Apr 14th, 2009
0

Re: c++ problem need help pls

C++ Syntax (Toggle Plain Text)
  1. while(ordnum != 99999);

if you put a semi colon at the end of while loop that means the loop doesnt have ne statements incorporated in them .
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Apr 14th, 2009
0

Re: c++ problem need help pls

Click to Expand / Collapse  Quote originally posted by rahul8590 ...
C++ Syntax (Toggle Plain Text)
  1. while(ordnum != 99999);

if you put a semi colon at the end of while loop that means the loop doesnt have ne statements incorporated in them .
Nice catch.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Apr 14th, 2009
0

Re: c++ problem need help pls

Click to Expand / Collapse  Quote originally posted by ithelp ...
Nice catch.
No it's not. He just posted the exact same thing as 'unbeatable0' posted 10 hours earlier, so I wouldn't call it 'a nice catch' at all.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Apr 15th, 2009
0

Re: c++ problem need help pls

am i stupid?
thanks for everyone's replying
especislly unbeatable0
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wuzj1988 is offline Offline
2 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC