11266

Program isn't printing correctly

I believe my program is still going through the if statements after it is declared invalid. It should print invalid filling status or invalid exemption status on the same line with the id. Buts its printing the valid statement above the the id and still displaying the taxableIncome and taxRate and TaxAmount. while(fscanf(tax,"\n%d %c %d %d ",&taxpayerId, &fillingStatus, &grossIncome, &taxExemptions) != EOF) {

fillingStatus = toupper(fillingStatus); if ( fillingStatus == 'S') { taxableIncome = grossIncome - 3000 - 1000 * taxExemptions; } else if ( fillingStatus == 'M') { taxableIncome = grossIncome - 3000 - 1000 * taxExemptions; } else if ( fillingStatus == 'J') { taxableIncome = grossIncome - 6000 - 1000 * taxExemptions; } if (taxableIncome < 0) { taxableIncome = 0; } if ( fillingStatus == 'S') { if (taxableIncome < 5000) { taxRate = 0.15; } if (taxableIncome > 20000) { taxRate = 0.31; } else { taxRate = 0.22; } } else if ( fillingStatus == 'M') { if (taxableIncome < 10000) { taxRate = 0.15; } if (taxableIncome > 40000) { taxRate = 0.31; } else { taxRate = 0.22; } } else if ( fillingStatus == 'J') { if (taxableIncome < 7000) { taxRate = 0.17; } if (taxableIncome > 25000) { taxRate = 0.33; } else { taxRate = 0.24; } } else { printf("\n %d **** Invalid filling status ****",taxpayerId); continue; } if (taxExemptions > 12 || taxExemptions < 0) { printf("\n %d **** Invalid number of exemptions ****", taxpayerId); } else { taxAmount = taxableIncome * taxRate; printf("\n %d %15.2f %15.2f %18.2f",taxpayerId,taxableIncome,taxRate,taxAmount); }

Output is suppose to look like this:

Taxpayer ID Taxable Income Tax Rate Tax Amount ----------- -------------- -------- ---------- 111111111 4000.00 0.15 600.00 222222222 36500.00 0.22 8030.00 333333333 19152.00 0.24 4596.48 444444444 **** Invalid filing status **** 555555555 67000.00 0.31 20770.00 666666666 53197.00 0.33 17555.01 777777777 19000.00 0.22 4180.00 888888888 **** Invalid number of exemptions **** 999999999 46308.00 0.33 15281.64 101010101 91602.00 0.31 28396.62 121212121 9525.00 0.15 1428.75 131313131 0.00 0.17 0.00

My out put look like this:

Taxpayer ID Taxable Income Tax Rate Tax Amount ----------- -------------- -------- ----------

111111111 4000.00 0.22 880.00 222222222 36500.00 0.31 11315.00 333333333 19152.00 0.24 4596.48 444444444 **** Invalid filing status **** 555555555 67000.00 0.31 20770.00 666666666 53197.00 0.24 12767.28 777777777 **** Invalid number of exemptions **** 888888888 3543.00 0.22 999999999 46308.00 0.24 779.46 101010101 91602.00 0.22 11113.92 121212121 9525.00 0.31 2952.75 131313131 0.00 0.24 0.00

Answer1:

fillingStatus == 's' || 'S'

The above condition does not do what you appear to think it does. Try:

fillingStatus == 's' || fillingStatus == 'S'

The reason is that the == operator only compares one pair of values at a time. So your expression reads something like:

if (fillingStatus is 's') or ('S' is nonzero)

which is always true (since 'S' is always nonzero). The corrected expression reads like:

if (fillingStatus is 's') or (fillingStatus is 'S')

It might be worth noting that some other languages have less verbose ways of expressing this condition than the above C++ code. For example, in Python you can write:

if fillingStatus in ('s', 'S'):

which tests fillingStatus for either 's' or 'S'.

Answer2:

You might consider doing the following after the scanf:

fillingStatus = toupper(fillingStatus);

Then only test for the uppercase characters:

fillingStatus == 'S'

You need to fix your 'ifs' as suggested and print output like the following. It won't be quite right until you've fixed the 'ifs', though.

if (taxExemptions > 12 || taxExemptions < 0) { printf("\n %d \t**** Invalid exemptions status ****", taxpayerId); } else { taxAmount = taxableIncome * taxRate; printf("\n %d \t %.2f \t\t %.2f \t %15.2f",taxpayerId,taxableIncome,taxRate,taxAmount); }

The more I look at this code, the more I'm confused too. What should the tax rate be for someone with taxableIncome of 1 and fillingStatus == 'S'. I think it's going to be 0.31, since it's less than 20000. Probably not what you're going for. I think you're missing some 'else if' statments.

Answer3:

change this

else { printf("\n**** Invalid filling status ****"); } if (taxExemptions > 12 || taxExemptions < 0) { printf("\n**** Invalid exemptions status ****"); }

to

else { printf("\n**** Invalid filling status ****"); continue; } if (taxExemptions > 12 || taxExemptions < 0) { printf("\n**** Invalid exemptions status ****"); continue; }

Answer4:

You can't use commas inside numbers in C++. The comma is the comma operator.

So instead of 7,000 write 7000.

if(taxable_income>7,000) { x; }

Would never execute x;

Answer5:

Edit: based on the updated question, it is clear that the problem lies not with the horrendous if statements, but with the input data - you are picking the wrong record as having invalid number of exemptions. This means two things:

    <li>

    you should have posted this earlier and saved people a lot of time looking in the wrong place

    </li> <li>

    you need to show the data that you are consuming if you want an opinion on why you are flagging the wrong record.

    </li> </ul>

    I've deleted my original answer. Next time please post <strong>ALL</strong> the relevant details.

Recommend

  • Reinforcement learning algorithm using turtle graphics not functioning
  • c++ structure: repeat set of functions at a certain time rate
  • Caffe, setting custom weights in layer
  • Program isn't printing correctly
  • Q-learning in neural network not 'learning'
  • Migrate Java Applet to what/where?
  • 'init is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memo
  • Having trouble initiating an array in a structure
  • STFT Clarification (FFT for real-time input)
  • Windows cpp target slows down in higher resolutions
  • Data not written to filesink in gnuradio
  • Airflow latency between tasks
  • Subprocess in Reading Serial Port Read
  • Finding the frequency per second from an audio file
  • Comparing two JPEG IMAGES and displaying differences
  • Cypher: Finding movies that haven't been rated yet by particular user
  • Performance degradation for Django with Gunicorn deployed into Heroku
  • How to add some text in google piechart pieHole
  • In App Link To Rate An App
  • Timer inside Android service
  • Pydub - How to change frame rate without changing playback speed
  • Validating multiple polymorphic values using Prismatic Schema
  • SoX running slow using a ProcessBuilder
  • Pass by Value and Pass by Reference
  • Play WS API: throttling request rates
  • WooCommerce creating Product Custom Attributes fields programatically?
  • Gmail API quota usage check
  • Disable tax programmatically for a specific user role
  • html/css: how to create a hexagonal image-placeholder
  • How to update powerpivot pivot table filter via cell reference?
  • Single virtual attribute definition for multiple fields
  • How to get file download speed (transfer rate) with php?
  • Paypal 'Buy Now' button not letting me dynamically set the price
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • .NET video play library which allows to change the playback rate?
  • Time out Error in send mail
  • ApplePay PKPaymentAuthorizationViewController always shows processing
  • Alternative to overridePendingTransition() - Android
  • angularjs unit test when to use $rootScope.$new()
  • Net Present Value in Excel for Grouped Recurring CF