944,122 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2742
  • C++ RSS
Oct 15th, 2007
0

Cable Bill Program

Expand Post »
I have this program for computing an individual's cable bill, then summing each column of values and calculating the average of each column. I got a 100 on the assignment, the program works fine, but this time we have to use functions. I created my functions and the program, again, works fine. Only problem is I have to convert money values into integers for dollars and cents and print them explicitly.

My problem is I don't know where to keep money values in doubles and when to actually convert them to integers. Ugh, this is so frustrating.

C++ Syntax (Toggle Plain Text)
  1. int main() {
  2.  
  3. // Constants
  4. double UTILITYFEE = 3.00; // convert into cents
  5. double TAX = 0.0012; // convert into cents
  6.  
  7. // Input Variables
  8. string CustomerName; // Customer's first name
  9. string AccountNo; // Customer's last name
  10. string Date; // Date
  11. double Outlets; // Number of Outlets
  12. char Cable; // Type of cable service
  13. ifstream In; // input stream
  14. ofstream Out; // output stream
  15.  
  16. // Local Variables
  17. int NumCustomers; // number of customers
  18. double ServiceCharge; // charge for N, B, or P
  19. double RegTax; // tax on services
  20. double OutletCharge; // number of outlets over one
  21. int TotalOutletCharge; // total charge of outlets
  22. int TotalCustomerCharge; // total charge of services for customer
  23. int TotalServiceCharge; // total charge of all services
  24. int TotalUtilityCharge; // total charge of utility tax
  25. int TotalTax; // total regulatory tax
  26. int TotalFee; // total charged to customer
  27. double AverageOutlet; // average number of outlets
  28. double AverageService; // average service charge
  29. double AverageUtility; // average utility fee
  30. double AverageTax; // average regulatory tax
  31. double AverageFee; // average total fee
  32.  
  33. // Initialize Variables
  34. NumCustomers = 0;
  35. TotalOutletCharge = 0;
  36. TotalCustomerCharge = 0;
  37. TotalServiceCharge = 0;
  38. TotalUtilityCharge = 0;
  39. TotalTax = 0;
  40. TotalFee = 0;
  41. AverageOutlet = 0;
  42. AverageService = 0;
  43. AverageUtility = 0;
  44. AverageTax = 0;
  45. AverageFee = 0;
  46.  
  47. // Open input and output file streams
  48. In.open("CableRecs.txt");
  49. Out.open("CableBills.txt");
  50.  
  51. Out << fixed << showpoint; // enables setprecision for output file
  52.  
  53. // Print error message if input file not found
  54. if (In.fail()) {
  55. cout << "Input file not found" << endl;
  56. cout << "Exiting..." << endl;
  57.  
  58. return EXIT_FAILURE;
  59. }
  60.  
  61. // Print output header
  62. PrintHeader( Out );
  63.  
  64. // Priming Read
  65. In.ignore(35, '\n');
  66. getline(In, CustomerName, '\t');
  67. getline(In, AccountNo, '\t');
  68. getline(In, Date, '\t');
  69. In >> Outlets >> Cable;
  70.  
  71. while ( In ) {
  72. NumCustomers++; // count number of customers
  73. CalcOutletCharge( Outlets, OutletCharge ); // calculate outlet charge
  74. SumTotal( OutletCharge, TotalOutletCharge ); // sum total outlet charge
  75. SumTotal( UTILITYFEE, TotalUtilityCharge ); // sum total utility fee
  76.  
  77. PickService( In, Cable, ServiceCharge ); // pick service plan
  78.  
  79. CalcRegTax( ServiceCharge, OutletCharge, TAX, RegTax ); // calculate regulatory tax
  80. CalcTotalCustomerCharge( ServiceCharge, OutletCharge, UTILITYFEE, RegTax, TotalCustomerCharge ); // calculate total customer charges
  81. SumTotal( RegTax, TotalTaxDollars, TotalTaxCents ); // sum total tax
  82. SumTotal( TotalCustomerCharge, TotalFee ); // sum total customer charges
  83. SumTotal( ServiceCharge, TotalServiceCharge ); // sum total service charges
  84.  
  85. PrintResults( Out, CustomerName, AccountNo, Date, OutletCharge, ServiceCharge, UTILITYFEE, RegTax, TotalCustomerCharge ); // print results to output
  86.  
  87. // get new line of input
  88. getline(In, CustomerName, '\t');
  89. getline(In, AccountNo, '\t');
  90. getline(In, Date, '\t');
  91. In >> Outlets >> Cable;
  92.  
  93. }
  94.  
  95. CalcAverage( TotalOutletCharge, NumCustomers, AverageOutlet ); // average outlet charge
  96. CalcAverage( TotalServiceCharge, NumCustomers, AverageService ); // average service charge
  97. CalcAverage( TotalUtilityCharge, NumCustomers, AverageUtility ); // average utility charge
  98. CalcAverage( TotalTax, NumCustomers, AverageTax ); // average tax charge
  99. CalcAverage( TotalFee, NumCustomers, AverageFee ); // average fee charge
  100.  
  101. // print rest of output file
  102. PrintCloser( Out, TotalOutletCharge, TotalServiceCharge, TotalUtilityCharge,
  103. TotalTax, TotalFee, AverageOutlet, AverageService, AverageUtility,
  104. AverageTax, AverageFee );
  105.  
  106. return EXIT_SUCCESS;
  107.  
  108. }
  109.  
  110.  
  111. /////////////////////////////////////////////////////////////////////////// PrintHeader
  112. // Prints header of output file
  113. //
  114. // Parameters:
  115. // Out file stream to be printed to
  116. //
  117. // Returns: header of output file
  118. //
  119. // Pre: ofstream opened
  120. // Post: header of output file printed
  121. //
  122. void PrintHeader( ofstream& Out)
  123. {
  124.  
  125. Out << "Programmer: " << "Ryan Dougherty" << endl;
  126. Out << "CS 1044 Project 4 Fall 2007" << endl << endl;
  127.  
  128. Out << "Adufia Customer Billing Report:" << endl << endl;
  129.  
  130. Out << left;
  131. Out << setw(24) << "Customer"
  132. << setw(18) << "Account"
  133. << setw(14) << "Billing"
  134. << setw(8) << "Outlets"
  135. << setw(8) << "Service"
  136. << setw(8) << "Utility"
  137. << setw(11) << "Regulatory"
  138. << setw(6) << "Total" << endl;
  139.  
  140. Out << setw(24) << "Name"
  141. << setw(18) << "Number"
  142. << setw(14) << "Date"
  143. << setw(8) << "Charge"
  144. << setw(8) << "Plan"
  145. << setw(8) << "Tax"
  146. << setw(11) << "Tax"
  147. << setw(6) << "Fee" << endl;
  148.  
  149. Out << "________________________________________________________________________________________________"
  150. << endl;
  151. }
  152.  
  153. /////////////////////////////////////////////////////////////////////////// CalcOutletCharge
  154. // Calculates charge for outlets used over 1
  155. //
  156. // Parameters:
  157. // int number of outlets used
  158. //
  159. // Returns: charge for outlets in cents
  160. //
  161. // Pre: outlets read from input file
  162. // Post: outlet charge stored into OutletCharge
  163. //
  164. void CalcOutletCharge( double Outlets, double& OutletCharge )
  165. {
  166. OutletCharge = (Outlets - 1.0); // will calculate outlet charge in cents
  167. }
  168.  
  169. /////////////////////////////////////////////////////////////////////////// SumTotal
  170. // keeps a running total of a sum of numbers
  171. //
  172. // Parameters:
  173. // int number to be summed
  174. // int& sum of the numbers
  175. //
  176. // Returns: sum stored into variable
  177. //
  178. // Pre: sum is initialized, number to be summed has value
  179. // Post: sum is updated
  180. //
  181. void SumTotal( double Number, double& Sum )
  182. {
  183. int Sum;
  184. int Temp;
  185.  
  186. Temp = Number * 100;
  187. Sum = Sum + Number;
  188.  
  189. Dollars = Sum / 100;
  190. Cents = Sum % 100;
  191. }
  192.  
  193. /////////////////////////////////////////////////////////////////////////// PickService
  194. // picks the service plan/charge for each customer based on a char value
  195. //
  196. // Parameters:
  197. // ifstream& for if statement
  198. // char determines service plan type
  199. // int& service plan charge, in cents
  200. //
  201. // Returns: service plan charge in cents
  202. //
  203. // Pre: char value is read from input file
  204. // Post: service plan is determined for customer
  205. //
  206. void PickService( ifstream& In, char Cable, double& ServiceCharge )
  207. {
  208. int temp;
  209.  
  210. if (Cable == 'N')
  211. ServiceCharge = 14.95; // converts service charge to cents
  212. else if (Cable == 'B')
  213. ServiceCharge = 34.95; // converts service charge to cents
  214. else if (Cable == 'P')
  215. ServiceCharge = 69.95; // converts service charge to cents
  216. }
  217.  
  218. /////////////////////////////////////////////////////////////////////////// CalcRegTax
  219. // calculates regulatory tax for customer
  220. //
  221. // Parameters:
  222. // int charge for service
  223. // int charge for outlets
  224. // int tax applied
  225. // int& regulatory tax calculated
  226. //
  227. // Returns: regulatory tax to be added to customer's bill
  228. //
  229. // Pre: all variables have values stored, regtax intialized
  230. // Post: regtax given new value for customer
  231. //
  232. void CalcRegTax(double ServiceCharge, double OutletCharge, double TAX, double& RegTax)
  233. {
  234. RegTax = (ServiceCharge + OutletCharge) * TAX; // tax is converted to cents
  235.  
  236. }
  237.  
  238. /////////////////////////////////////////////////////////////////////////// CalcTotalCustomerCharge
  239. // calculates total bill for customer
  240. //
  241. // Parameters:
  242. // int service charge in cents
  243. // int outlet charge in cents
  244. // int regulatory taxin cents
  245. // int utility fee in cents
  246. // int& total charge for customer
  247. //
  248. // Returns: total charge for individual customer
  249. //
  250. // Pre: all variables have values stored, totalcustomercharge initialized
  251. // Post: totalcustomercharge stored with new value
  252. //
  253. void CalcTotalCustomerCharge( double ServiceCharge, double OutletCharge, double UTILITYFEE, double RegTax, double& TotalCustomerCharge )
  254. {
  255. TotalCustomerCharge = ServiceCharge + OutletCharge + UTILITYFEE + RegTax;
  256. }
  257.  
  258. /////////////////////////////////////////////////////////////////////////// PrintResults
  259. // prints results for each customer
  260. //
  261. // Parameters:
  262. // ofstream& output file stream
  263. //
  264. // Returns: printed values of results
  265. //
  266. // Pre: all variables have values
  267. // Post: results printed to output file
  268. //
  269. void PrintResults( ofstream& Out, string CustomerName, string AccountNo, string Date, double OutletCharge, double ServiceCharge, double UTILITYFEE, double RegTax, double TotalCustomerCharge )
  270. {
  271. Out << left;
  272. Out << setw(24) << CustomerName;
  273. Out << setw(18) << AccountNo;
  274. Out << setw(15) << Date;
  275. Out << setw(8) << setprecision(2) << OutletCharge;
  276. Out << setw(8) << setprecision(2) << ServiceCharge;
  277. Out << setw(8) << setprecision(2) << UTILITYFEE;
  278. Out << setw(11) << setprecision(2) << RegTax;
  279. Out << setw(6) << setprecision(2) << TotalCustomerCharge << endl;
  280. }
  281.  
  282. /////////////////////////////////////////////////////////////////////////// CalcAverage
  283. // calculates the average of a sum and the number of customers
  284. //
  285. // Parameters:
  286. // int total to be averaged
  287. // int number of customers
  288. // int& average value in cents
  289. //
  290. // Returns: average value in cents
  291. //
  292. // Pre: all variables have values
  293. // Post: average is calculated and stored into average
  294. //
  295. void CalcAverage( double Total, int Divisor, double& Average )
  296. {
  297. Average = Total / Divisor;
  298. }
  299.  
  300. /////////////////////////////////////////////////////////////////////////// PrintCloser
  301. // prints the rest of the output file
  302. //
  303. // Parameters:
  304. // ofstream& output file stream
  305. //
  306. // Returns: rest of output file
  307. //
  308. // Pre: all variables have values
  309. // Post: all of output file is printed
  310. //
  311. void PrintCloser( ofstream& Out, double TotalOutletCharge, double TotalServiceCharge, double TotalUtilityCharge, double TotalTax, double TotalFee, double AverageOutlet, double AverageService, double AverageUtility, double AverageTax, double AverageFee )
  312. {
  313.  
  314. Out << "________________________________________________________________________________________________"
  315. << endl;
  316. Out << left;
  317. Out << setw(56) << "Totals:"
  318. << setw(8) << setprecision(2) << TotalOutletCharge
  319. << setw(8) << setprecision(2) << TotalServiceCharge
  320. << setw(8) << setprecision(2) << TotalUtilityCharge
  321. << setw(11) << setprecision(2) << TotalTax
  322. << setw(6) << setprecision(2) << TotalFee << endl;
  323. Out << setw(56) << "Averages:"
  324. << setw(8) << setprecision(2) << AverageOutlet
  325. << setw(8) << setprecision(2) << AverageService
  326. << setw(8) << setprecision(2) << AverageUtility
  327. << setw(11) << setprecision(2) << AverageTax
  328. << setw(6) << setprecision(2) << AverageFee << endl;
  329. }

HELP
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dukedoc is offline Offline
5 posts
since Oct 2007
Oct 15th, 2007
0

Re: Cable Bill Program

you can use modf to split a double into its two parts
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Oct 16th, 2007
0

Re: Cable Bill Program

Write another function that simply prints out the double values by converting to ints. Then at all places you output the values, call the function instead.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

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:
Previous Thread in C++ Forum Timeline: c++ mouse
Next Thread in C++ Forum Timeline: [C++] append() to each row of a text file





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


Follow us on Twitter


© 2011 DaniWeb® LLC