First year assigment on reading file, sorting and outputting invoice

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

Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

 
0
  #11
May 28th, 2006
You have failed to use code tags in the 3 posts you have posted code. Use [code][/code] tags the next time. There is a watermark in the message edit box telling you about it, so you can't say you didn't know.
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
  #12
May 28th, 2006
I'm sorry about that. I actually dont understand it fully.
I thought I did it, but obviously not.
Do I have to put a [] around everyline then?
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
  #13
May 28th, 2006
or just [ at the start and ] at the end?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

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

 
0
  #14
May 28th, 2006
Anything written between [code] [/code] will be considered as code.

For example

[code]

#include <iostream>

[/code]


will appear like

  1. #include <iostream>
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
  #15
May 29th, 2006
Shame I cant delete old posts, because I have improved my code.

Please ignore the last 2 codes.

I will update soon.
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
  #16
May 29th, 2006
Can someone suggest why this for loop is not working as it should?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7.  
  8. const int MAX_SIZE=5;
  9. char station [MAX_SIZE];
  10.  
  11. int count = 0;
  12.  
  13. do{
  14. cout<<"Enter station:"<<endl;
  15. cin>>station[count];
  16. double totalA = 0;
  17. if (station [MAX_SIZE] = 'A')
  18. {
  19. for (int i=0; i<count; i++)
  20. totalA= totalA + 2.20;
  21. cout<<totalA;
  22. }
  23. }
  24. while (count<MAX_SIZE);
  25.  
  26.  
  27. system ("pause");
  28. return 0;
  29. }


it returns a value of 0 everytime.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
1
  #17
May 29th, 2006
You have to increment count:

  1. count = count + 1;

Change this:

  1. char station [MAX_SIZE];

to this:

string station [MAX_SIZE];

and this:

  1. if (station [MAX_SIZE] = 'A')

to this:

if (station [MAX_SIZE] == "A")
*Voted best profile in the world*
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
  #18
May 29th, 2006
if (station [MAX_SIZE] = 'A')

In the above line, station[MAX_SIZE] doesn't exist or is out of bounds/illegal, is bad. The largest valid index is MAX_SIZE minus 1. You probably don't even want MAX_SIZE minus 1 because it won't be initialized until the fifth time through the do/while loop. You probably want count instead of MAX_SIZE.

Keeping station an array of char isn't wrong. Making sation an array of strings isn't necessary, but will also work.

Do yourself and others trying to help by using good indenting and spaces habits. It's much easier to read.
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
  #19
May 30th, 2006
Geez,
thanks everyone.
I have been busy and come up with this. about 5 lines give me errors. So I have not been able to test the thing yet on the data. But I am excited about getting closer!

I have also worked on my indentations. Please check it out.

  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);
  56. time [count]= inputString.substr(11,16-11);
  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 (int& MAX_SIZE, char& station, int& count, double& totalA, double& totalB, double& totalC, double& totalD)
  66.  
  67. {
  68. station [MAX_SIZE];
  69.  
  70. count = 0;
  71. totalA = 0;
  72. totalB = 0;
  73. totalC = 0;
  74. totalD = 0;
  75.  
  76. do{
  77.  
  78. if (station [count] == 'A')
  79. {
  80. totalA= totalA + 2.20;
  81. cout<<totalA;
  82. }
  83.  
  84. if (station [count] == 'B')
  85. {
  86. totalB= totalB + 2.80;
  87. cout<<totalB;
  88. }
  89.  
  90. if (station [count] == 'C')
  91. {
  92. totalC= totalC + 2.30;
  93. cout<<totalC;
  94. }
  95.  
  96. if (station [count] == 'D')
  97. {
  98. totalD= totalD + 3.80;
  99. cout<<totalD;
  100. }
  101.  
  102. }
  103. while (count<MAX_SIZE);
  104. }
  105.  
  106. void resetControlTotals(double& controlTotal)
  107.  
  108. {
  109. controlTotal = 0;
  110. }
  111.  
  112. void printTotalInvoice (int& MAX_SIZE, char& station, double& totalA, double& totalB, double& totalC, double& totalD, double& totalInvoice, double& controlTotal)
  113.  
  114. {
  115.  
  116. station [MAX_SIZE];
  117.  
  118. totalA = 0;
  119. totalB = 0;
  120. totalC = 0;
  121. totalD = 0;
  122. totalInvoice = 0;
  123.  
  124. totalInvoice = totalA + totalB + totalC + totalD;
  125. cout<<"TOTAL:\t\t\t\t\t\t\$"<<totalInvoice<<endl;
  126. cout<<"\t\t\t\t\t\t_______"<<endl;
  127. cout<<"\t\t\t\t\t\t_______"<<endl;
  128. cout<<"We thankyou for your prompt payment."<<endl;
  129. resetControlTotals(controlTotal);
  130.  
  131. }
  132.  
  133. int main ()
  134. {
  135. int id;
  136. int prevId;
  137. string name;
  138. string prevName;
  139. double invoiceTotal;
  140. double controlTotal;
  141. int pageCount;
  142. int lineCount;
  143.  
  144. prevId =id;
  145. prevName=name;
  146. while(more)
  147. {
  148. if (id!=prevId)
  149. printTotalInvoice (MAX_SIZE, station, totalA, totalB, totalC, totalD, totalInvoice);
  150. prevId= id;
  151. prevName = name;
  152. }
  153. if (lineCount >MAX_DETAIL_LINES)
  154. printGenericHeading(pageCount, lineCount);
  155. printCustomerHeading(pageCount, lineCount);
  156. readDateTime (MAX_LINE, MAX_SIZE, date, time, inputString, count, position);
  157. calculateTotalStation (MAX_SIZE, station, count, totalA, totalB, totalC, totalD);
  158. resetControlTotals(controlTotal);
  159. printTotalInvoice (MAX_SIZE, station, totalA, totalB, totalC, totalD, totalInvoice);
  160.  
  161. system("pause");
  162. return 0;
  163. }
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
  #20
May 30th, 2006
By the way, as per iamthwee, I changed the station declaration to be string type. (just now). Sorry its not included above and I'll avoid resubmitting to avoid clutter.
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: 5650 | 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