| | |
Printing gets me incorrecting answers
![]() |
•
•
Join Date: Dec 2008
Posts: 12
Reputation:
Solved Threads: 0
This is the problem, when I run it, there are no errors. However when I check the reportfile, I end up with the wrong numbers from the calculations.
The calculator:
The printer file
What was printed on reportfile:
The 'Totals' and 'Averages' section are completely out of whack. I'm guessing is a Print_Summary_To_File problem.
The calculator:
C Syntax (Toggle Plain Text)
void AddDetailToAccumulators(float *totfedtax, float *totssitax, float *totstatetax,float *totrate, float *totreg, float *totovt, float *totgross,float *totpay,float *totdeferred, float gross, float federaltax, float statetax, float ssitax,float hours, float payrate,float deferred,float reghrs,float ovthrs,float netpay,float *avgfedtax, float *avgssitax, float *avgstatetax,float *avgrate, float *avgreg, float *avgovt, float *avggross,float *avgpay,float *avgdeferred) { *totfedtax+=federaltax; *totstatetax+=statetax; *totssitax+=ssitax; *totrate+=payrate; *totdeferred+=deferred; *totreg+=reghrs; *totovt+=ovthrs; *totgross+=gross; *totpay+=netpay; *avgfedtax=*totfedtax/MAX; *avgstatetax=*totstatetax/MAX; *avgssitax=*totssitax/MAX; *avgrate=*totrate/MAX; *avgdeferred=*totdeferred/MAX; *avgreg=*totreg/MAX; *avgovt=*totovt/MAX; *avggross=*totgross/MAX; *avgpay=*totpay/MAX; }
The printer file
C Syntax (Toggle Plain Text)
void Print_Summary_To_File(float totfedtax, float totssitax, float totstatetax,float totrate, float totreg, float totovt, float totgross,float totpay,float totdeferred,float avgfedtax, float avgssitax, float avgstatetax,float avgrate, float avgreg, float avgovt, float avggross,float avgpay,float avgdeferred,FILE *reportfile) { fprintf(reportfile, "%16s%15.2f%9.2f%9.2f%9.2f", "Totals", totrate, totreg, totgross, totfedtax); fprintf(reportfile, "%8.2f%9.2f \n%40.2f%18.2f%8.2f\n\n", totssitax, totpay, totovt,totstatetax, totdeferred); fprintf(reportfile, "%18s%13.2f%9.2f%9.2f%9.2f", "Averages", avgrate, avgreg, avggross, avgfedtax); fprintf(reportfile, "%8.2f%9.2f \n%40.2f%18.2f%8.2f\n\n\n\n\n\n", avgssitax, avgpay, avgovt, avgstatetax, avgdeferred); }
What was printed on reportfile:
•
•
•
•
Employee Pay Reg Hrs Gross Fed SSI Net
Name Rate Ovt Hrs Pay State Defr Pay
================= ===== ======= ======= ======= ====== ======
Doe, John 17.00 40.00 845.75 119.36 61.67 606.36
6.50 8.36 50.00
Whittle, Ed 11.50 25.50 293.25 43.99 22.73 223.46
0.00 3.08 0.00
Marion, Louise 13.00 40.00 520.00 63.00 32.55 320.04
0.00 4.41 100.00
Prentiss, Paula 15.75 40.00 878.06 112.96 58.36 573.83
10.50 7.91 125.00
Davidson, Carl 8.75 38.00 332.50 47.63 24.61 241.93
0.00 3.33 15.00
Totals -107374112.00-107373992.00-107371296.00-107373792.00-107373976.00-107372208.00
-107374160.00 -107374152.00-107373888.00
Averages -21474822.00-21474798.00-21474260.00-21474758.00-21474796.00-21474442.00
-21474832.00 -21474830.00-21474778.00
Last edited by elitedragoon; Dec 14th, 2008 at 12:47 pm.
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
Well, you could look at the values in the debugger if you have one, or you could add additional print statements to better confirm the values. If you come to the conclusion that the values are correct then you have to chase back to where they came from to see where they were last correct.
(This is why developing and testing iteratively makes so much sense, you only have to look at the new stuff if something stops working.)
it almost doesn't make any sense to calculate the average as you go along (unless you need intermediate averages).
In the AddDetailToAccumulators() you are dividing the total by MAX to generate the average. Are you always going to read MAX values?
Secondarily, the AddDetailToAccumulators() adds to the totals...did you ensure that you initialized the totals to zero before you started?
(This is why developing and testing iteratively makes so much sense, you only have to look at the new stuff if something stops working.)
it almost doesn't make any sense to calculate the average as you go along (unless you need intermediate averages).
In the AddDetailToAccumulators() you are dividing the total by MAX to generate the average. Are you always going to read MAX values?
Secondarily, the AddDetailToAccumulators() adds to the totals...did you ensure that you initialized the totals to zero before you started?
Last edited by Murtan; Dec 15th, 2008 at 12:28 am. Reason: sp
I am posting this, absolutely not to find error in your code(coz its really hard to understant your code) but to tell you, how you can improve your coding style so that the error rate becomes less.
You have been suggested to use struct in place of passing so many parameters to a function.
Try to code simple so that others can understand your code easily.
At least you do this, to make ur and our life easier.
You have been suggested to use struct in place of passing so many parameters to a function.
Try to code simple so that others can understand your code easily.
At least you do this, to make ur and our life easier.
Last edited by Luckychap; Dec 15th, 2008 at 6:14 am.
When you think you have done a lot, then be ready for YOUR downfall.
![]() |
Other Threads in the C Forum
- Previous Thread: Cursor-based implementation of list
- Next Thread: Text Extraction From Image
| Thread Tools | Search this Thread |
* adobe ansi api array binarysearch centimeter changingto char character cm convert copyanyfile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches infiniteloop input interest intmain() iso keyboard kilometer linked linkedlist linux linuxsegmentationfault list looping loopinsideloop. lowest match meter microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf posix power process program programming pyramidusingturboccodes read recv recvblocked repetition research reversing scanf scheduling segmentationfault send single socket socketprograming socketprogramming stack standard string suggestions threads unix urboc user voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi





