0

I have been trying to figure out the correct calculations for the this problem and can't seem to figure out how to get it right using the following information. I have posted my code below as well. I don't understand what to do to get the actual tax amount for any given income. This is probably more of a math issue, but any help would be appreciated!

Calculate taxes using this information.

'''
//Write a program that computes taxes.

#include <iostream>
#include<string>
using namespace std;

int main() 
{

double tax1 = 0;
double tax2 = 0;
double tax3 = 0;

double income = 0;
string marital_status;

cout << "Please enter your income: ";
  cin >> income;

cout << "Please enter s for single, or m for married: ";
  cin >> marital_status;

if (marital_status == "s")
  {
    if (income <= 8000)
      {
        tax1 = income * .10;
      }
      else if (income <= 32000)
      {
         tax1 = income * .10;
         tax2 = income * .15 + 800;
      }
      else
      {
        tax1 = income * .10; 
        tax2 = income * .15 + 800;
        tax3 = income * .25 + 4400;
      }
  }
else
{
  if (income <= 16000)
      {
        tax1 = income * .10;
      }
      else if (income <= 64000)
      {
         tax1 = income * .10;
         tax2 = income * .15 + 1600;
      }
      else
      {
        tax1 = income * .10;
        tax2 = income * .15 + 1600;
        tax3 = income * .25 + 8800; 
      }
}

   double total_tax = tax1 + tax2 + tax3;

   cout << "The tax is $" << total_tax << endl;

return 0;
}
'''
Isabel
  • 25
  • 4
  • You've just misunderstood the table your reading. If income is between 8000 and 32000 and single, the TOTAL tax is 800 + 15% of the amount above 8000. You are doing 800 + 15% of the total income + additional 10% of the total income. – super Apr 02 '20 at 00:36
  • 1
    You want something like `tax = 800 + (income - 8000) * .15;` – super Apr 02 '20 at 00:39

1 Answers1

0

The problem has been pointed out in the comment as it is just due to misinterpretation of the table. Currently, you have hard coded most numbers in the rule directly and hence if the threshold 8000 change in the future, you have to change the corresponding value in multiple places. You might like to define a variable to store those values.

#include <iostream>
#include<string>
using namespace std;

int main() 
{

double tax = 0;
double first_threshold = 8000;
double second_threshold = 32000;
double first_percent = .1;
double second_percent = .15;
double third_percent = .25;

double income = 0;
string marital_status;

cout << "Please enter your income: ";
  cin >> income;

cout << "Please enter s for single, or m for married: ";
  cin >> marital_status;

if (marital_status == "s")
  {
    if (income <= first_threshold)
      {
        tax = income * first_percent;
      }
      else if (income <= second_threshold)
      {
         tax = (income - first_threshold) * second_percent + first_threshold * first_percent;
      }
      else
      {
        tax = (income - second_threshold) * third_percent + first_threshold * first_percent + (second_threshold - first_threshold) * second_percent;
      }
  }
else
{
  if (income <= 2 * first_threshold)
      {
        tax = income * first_percent;
      }
      else if (income <= 2 * first_threshold)
      {
         tax = (income - 2*first_threshold) * second_threshold + 2 * first_threshold * first_percent;
      }
      else
      {
        tax = (income - 2 *second_threshold) * third_percent + 2* first_threshold * first_percent + 2*(second_threshold - first_threshold) * second_threshold;
      }
}

   cout << "The tax is $" << tax << endl;

return 0;
}

Also, notice that the threshold for married couple is twice the threshold for the singles. Hence, if we wish, we can make the code to be more compact as follows:

#include <iostream>
#include<string>
using namespace std;

int main() 
{

double tax = 0;
double first_threshold = 8000;
double second_threshold = 32000;
double first_percent = .1;
double second_percent = .15;
double third_percent = .25;

double income = 0;
string marital_status;

cout << "Please enter your income: ";
  cin >> income;

cout << "Please enter s for single, or m for married: ";
  cin >> marital_status;

if (marital_status == "m"){
    first_threshold *= 2;
    second_threshold *= 2;
}

if (income <= first_threshold)
      tax = income * first_percent;
else if (income <= second_threshold)
      tax = (income - first_threshold) * second_percent + first_threshold * first_percent;
else{
        tax = (income - second_threshold) * third_percent + first_threshold * first_percent + (second_threshold - first_threshold) * second_percent;
}

   cout << "The tax is $" << tax << endl;

return 0;
}
Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31