First year assigment on reading file, sorting and outputting invoice

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #21
May 30th, 2006
[B]Lerner[B],
If I make changes to make station[count], then will it be ok to have code like this for that section:

  1. void calculateTotalStation (int& MAX_SIZE, string& station, int& count, double& totalA, double& totalB, double& totalC, double& totalD)
  2.  
  3. {
  4. count = 0;
  5. station [count];
  6. totalA = 0;
  7. totalB = 0;
  8. totalC = 0;
  9. totalD = 0;
  10.  
  11. do{
  12.  
  13. if (station [count] == 'A')
  14. {
  15. totalA= totalA + 2.20;
  16. cout<<totalA;
  17. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #22
May 30th, 2006
I would have a series of arrays of int called stationA, stationB, stationC all initialized to zero. As I was reading the data from file, each time a stationA was found for any given ID i would increase the int at index i for stationA. Else if a stationB was found for any given ID I would increment the value at index of stationB with same index as given ID, etc. Then when I wanted to calculate the total of the invoice it would be a calculation such as:

amount to put at index i for array totalAmount is (number at index i in stationA * value of stationA) plus (number at index i in stationB * value of stationB) plus etc
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #23
May 30th, 2006
So my method wont work at all? That is a shame coz it seems so nice and logical to me. I'll start making changes....
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #24
May 31st, 2006
In reviewing your post with the "entire" program, it looks as though you have abanded the idea of storing all the data in parallel arrays and printing out the results all at once after the entire file has been read in favor of printing out an invoice for a given ID when a new ID has been found, which is fine, but you won't need arrays for stationA, stationB, etc. under the latter scenario.

I note that there is no mechanism to read from file in the program posted so that mechanism will need to be added at some point if you are going to be getting the data from a file.

It also looks like the while loop in main() is terminated early and the body of the if() statements terminate prematurely for lack of controlling brackets, but it's difficult to say for sure without comments.

In the following, it looks like you are trying to save all the stations for a given ID in an array and then calculate the station totals from there. That's viable. However, to pass an array of strings you would use string * station, not string & station and MAX_SIZE was declared global, so it doesn't need to be passed anywhere. (Globals should be avoided when possible, but this is one situation where I find using them at least tolerable).

  1. void calculateTotalStation (int& MAX_SIZE, string& station, int& count, double& totalA, double& totalB, double& totalC, double& totalD)
  2.  
  3. {
  4. //count appears to be the number of stations visit by this ID
  5. //the array of stations and the count are passed in, so the
  6. //following two lines aren't needed.
  7.  
  8. count = 0; //this would mean no stations will be visited
  9. station [count]; //to my knowledge this is an illegal statement
  10.  
  11. //this will set all station totals to 0
  12. totalA = 0;
  13. totalB = 0;
  14. totalC = 0;
  15. totalD = 0;
  16.  
  17. do
  18. {
  19. if (station [count] == 'A') //station[count] is a string so use "A"
  20. {
  21. totalA= totalA + 2.20;
  22. cout<<totalA;
  23. }

Here's how I would use an array of string called station to do the calculations:

  1. void calculateTotalStation (string * station, int count, double& totalA, double& totalB, double& totalC, double& totalD)
  2. {
  3. totalA = 0;
  4. totalB = 0;
  5. totalC = 0;
  6. totalD = 0;
  7.  
  8. int i = 0;
  9. do
  10. {
  11.  
  12. if (station [i] == "A")
  13. {
  14. totalA= totalA + 2.20;
  15. cout<<totalA;
  16. }
  17.  
  18. else if(station[i] == "B")
  19. .
  20. .
  21. .
  22. }while(i < count)
  23. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #25
May 31st, 2006
I really want to print out total for id xx is $....
Total for id YY is $....

I have amended code.
Is this looking closer to it?
I have made comments where I get errors. My declarations here and there are weird now...

  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. void printGenericHeading(int&, int&);
  6. void printCustomerHeading(int&, int&);
  7. void readDateTime (int& , int& , string& , string& , string& ,int& , int& );
  8. void calculateTotalStation (int& , char& , int& , double& , double& , double& , double& );
  9. void resetControlTotals(double&);
  10. void printTotalInvoice (int& , char& , double& , double& , double& , double& , double& );
  11.  
  12. const int MAX_DETAIL_LINES =100;
  13. const int MAX_LINE = 100;
  14. const int MAX_SIZE = 20;
  15. double TotalInvoice = 0;
  16. int pageCount = 0;
  17. int lineCount = 0;
  18. bool more= true;
  19.  
  20. void printGenericHeading(int& pageCount, int& lineCount)
  21. {
  22. pageCount = pageCount +1;
  23. cout<<"INVOICE FOR TOLL EXPENSES"<<endl;
  24. cout<<"_________________________"<<endl;
  25. cout<<"TAG\tCUSTOMER\tSTATION"
  26. <<"\t\t\t\t\n";
  27. cout<<"ID\tNAME\t\tID\n";
  28.  
  29. lineCount = 4;
  30. }
  31.  
  32. void printCustomerHeading(int& pageCount, int& lineCount)
  33. {
  34. pageCount = pageCount +1;
  35. cout<<"\t\t\t\t\n";
  36. cout<<"t\t\t\t\tDATE\t\tTIME\tAMOUNT\n\n";
  37.  
  38. lineCount = 3;
  39. }
  40.  
  41. void readDateTime (int& MAX_LINE, int& MAX_SIZE, string * date, string * time, string& inputString,int& count, int& position)
  42. {
  43.  
  44. date [MAX_SIZE];
  45. time [MAX_SIZE];
  46. inputString;
  47. count =0;
  48.  
  49. do {
  50. cerr<<"processing..."<<endl;
  51. getline(cin, inputString);
  52. if(cin.fail()) break;
  53.  
  54. int position = inputString.find ('/');
  55. date [count] = inputString.substr(0, 10); // this line gave error so I change string & to string * date)
  56. time [count]= inputString.substr(11,16-11); //this line gave error so I change string & to string * time)
  57. count++;
  58. }
  59. while (count<MAX_SIZE);
  60.  
  61. for (int i=0; i<count; i++)
  62. cout <<date[i]<<"\t"<<time[i]<<endl;
  63.  
  64. }
  65. void calculateTotalStation (string * station, int count, double& totalA, double& totalB, double& totalC, double& totalD, double& totalInvoice, double& controlTotal)
  66. {
  67. totalA = 0;
  68. totalB = 0;
  69. totalC = 0;
  70. totalD = 0;
  71.  
  72. int i = 0;
  73. do
  74. {
  75.  
  76. if (station [i] == "A")
  77. {
  78. totalA= totalA + 2.20;
  79. cout<<totalA;
  80. }
  81.  
  82. else if(station[i] == "B")
  83. {
  84. totalB= totalB + 2.80;
  85. cout<<totalB;
  86. }
  87. else if(station[i] == "C")
  88. {
  89. totalC= totalC + 2.30;
  90. cout<<totalC;
  91. }
  92. else if(station[i] == "D")
  93. {
  94. totalD= totalD + 3.80;
  95. cout<<totalD;
  96. }
  97.  
  98. totalInvoice = totalA + totalB + totalC + totalD;
  99. cout<<"TOTAL:\t\t\t\t\t\t\$"<<totalInvoice<<endl;
  100. cout<<"\t\t\t\t\t\t_______"<<endl;
  101. cout<<"\t\t\t\t\t\t_______"<<endl;
  102. cout<<"We thankyou for your prompt payment."<<endl;
  103. resetControlTotals(controlTotal);
  104.  
  105.  
  106. }while(i < count);
  107. }
  108.  
  109. void resetControlTotals(double& controlTotal)
  110.  
  111. {
  112. controlTotal = 0;
  113. }
  114.  
  115.  
  116.  
  117. int main ()
  118. {
  119. int id;
  120. int prevId;
  121. string name;
  122. string prevName;
  123. double invoiceTotal;
  124. double controlTotal;
  125. int pageCount;
  126. int lineCount;
  127.  
  128. prevId =id;
  129. prevName=name;
  130. while(more)
  131. {
  132. if (id!=prevId)
  133. calculateTotalStation (MAX_SIZE, station, count, totalA, totalB, totalC, totalD); //this line gives error (STATION UNDECLARED)
  134. prevId= id;
  135. prevName = name;
  136. }
  137. if (lineCount >MAX_DETAIL_LINES)
  138. printGenericHeading(pageCount, lineCount);
  139. printCustomerHeading(pageCount, lineCount);
  140. readDateTime (MAX_LINE, MAX_SIZE, date, time, inputString, count, position); // this line gives error of date undeclared
  141. calculateTotalStation (MAX_SIZE, station, count, totalA, totalB, totalC, totalD);// this line gives error of station undeclared
  142. resetControlTotals(controlTotal);
  143. printTotalInvoice (MAX_SIZE, station, totalA, totalB, totalC, totalD, totalInvoice);// this line gives error of station undeclared
  144.  
  145. system("pause");
  146. return 0;
  147. }

Thanks for your help....
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #26
May 31st, 2006
You asked if I had abandoned the idea of using parallel arrays.

I havent. I just am walking in the dark here and only getting to understand the whole concept of parallel arrays.

I hope this final version is pretty close to how it should be, since I have to hand it in tonight (Australia time). but I also have to 'clean up' my hierarchy chart, PSEUDOCODE, and do a deskcheck chart, once I am happy the program is as close as I can get it).

Thanks for your time.
So am I almost there?
Reply With Quote Quick reply to this message  
Reply

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




Views: 5649 | Replies: 25
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC