Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MAKE SURE IT RUNS PROPERLY You are asked to create a program that calculates a t

ID: 671207 • Letter: M

Question

MAKE SURE IT RUNS PROPERLY

You are asked to create a program that calculates a tax table for a city where there is a city, county, and state tax. The city tax rate is 3%, the county rate is 4%, and the state rate is 1%. The state rate is not applicable unless the purchase is over $ 1,000.00. Write a program that reads in dollar amounts from a file called dollars. txt, calculates the taxes owed, and prints the result to the screen. This code can be completed in your main function. The class for this program must be called Problem2A.To test your code, create a simple text file named dollars. txt and add the values as follows: 10.0 100.0 1000.0 2000.0 Create a new project and a new class called Problem2B. Copy the code from part (a) into your class. Modify your code so that instead of printing the results to the screen, you print them to a file called taxes. txt. Create a new project and a new class called Problem2C. Copy the code from part (b) to the new class. Modify the class as follows: Create a function called calculateTaxes that takes as an argument the dollar amount and returns an array with all the tax values. The calculations will be the same as in part (a) but the results must be returned in an array. This means that you can refactor (more) your code from the main class to the method with very little modification. The return array should have the following structure: [dollar amount, 3% tax, 4% tax, 1% tax or 0] Example output array for input of 10.0: [10.0,0.03,0.04,0.00] Call the calculateTaxes function from your main function and then print the results to the output file as in problem (b). Use the following guide for your main() function in part (c): // Open the input data file // Open the output data file // For each line in the data file, // call calculateTaxes // write the results to the output data file // Close the input and output files

Explanation / Answer

Program2A:

#include <iostream>
#include <fstream> //for input and output files
#include <iomanip>
using namespace std;
class Problem2A
{
   public:
   double tax;
   double amount;
   double cityTax;
   double countryTax;
   double stateTax;
};
int main()
{
   Problem2A pbm2;
   //ofstream outfile; //output file stream variable
ifstream infile; //input file stream variable
int cityTaxRate=3;
   int countryTaxRate=4;
   int stateTaxRate=1;
infile.open("dollars.txt");// create this file in proper location with data
cout<<endl<<endl<<"TAX CALCULATION";
   cout<<endl<<"===================================================================";
cout<<endl<<"Amount in $"<<" "<<"3 %Tax"<<" "<<"4 %Tax"<<" "<<"1 %Tax"<<" "<<"Total Tax";
   do
{
   pbm2.tax=0.0;  
    infile>>pbm2.amount;
    pbm2.cityTax=0;
    pbm2.cityTax=pbm2.amount*cityTaxRate/100;
    pbm2.countryTax=0;
    pbm2.countryTax=pbm2.amount*countryTaxRate/100;
   pbm2.tax=pbm2.cityTax + pbm2.countryTax;
   pbm2.stateTax=0.0;   
if(pbm2.amount>1000){
pbm2.stateTax=pbm2.amount*stateTaxRate/100;  
   pbm2.tax+=pbm2.stateTax;
   }
   cout<<endl<<pbm2.amount<<" "<<pbm2.cityTax<<" "<<pbm2.countryTax<<" "<<pbm2.stateTax<<" "<<pbm2.tax;
}while(!infile.eof());
cout<<endl<<"====================================================================";
infile.close();
   return 0;
}

Program2B:

#include <iostream>
#include <fstream> //for input and output files
#include <iomanip>
using namespace std;
class Problem2B
{
   public:
   double tax;
   double amount;
   double cityTax;
   double countryTax;
   double stateTax;
};
int main()
{
   Problem2B pbm2;
   ofstream outfile; //output file stream variable
ifstream infile; //input file stream variable
int cityTaxRate=3;
   int countryTaxRate=4;
   int stateTaxRate=1;
infile.open("dollars.txt");// create this file in proper location with data
outfile.open("tax.txt");
outfile<<endl<<endl<<"TAX CALCULATION";
   outfile<<endl<<"===================================================================";
outfile<<endl<<"Amount in $"<<" "<<"3 %Tax"<<" "<<"4 %Tax"<<" "<<"1 %Tax"<<" "<<"Total Tax";
   do
{
   pbm2.tax=0.0;  
    infile>>pbm2.amount;
    pbm2.cityTax=0;
    pbm2.cityTax=pbm2.amount*cityTaxRate/100;
    pbm2.countryTax=0;
    pbm2.countryTax=pbm2.amount*countryTaxRate/100;
   pbm2.tax=pbm2.cityTax + pbm2.countryTax;
   pbm2.stateTax=0.0;   
if(pbm2.amount>1000){
pbm2.stateTax=pbm2.amount*stateTaxRate/100;  
   pbm2.tax+=pbm2.stateTax;
   }
   outfile<<endl<<pbm2.amount<<" "<<pbm2.cityTax<<" "<<pbm2.countryTax<<" "<<pbm2.stateTax<<" "<<pbm2.tax;
}while(!infile.eof());
outfile<<endl<<"====================================================================";
infile.close();
outfile.close();
   return 0;
}

Program2C:

#include <iostream>
#include <fstream> //for input and output files
#include <iomanip>
using namespace std;
class Problem2C
{
   public:
   double tax[10];
   double amount[10];
   double cityTax;
   double countryTax;
   double stateTax;
   double** calculateTaxes(int);
};

double** Problem2C ::calculateTaxes(int n)
{
   int cityTaxRate=3;
   int countryTaxRate=4;
   int stateTaxRate=1;
   int i;
   double** taxArray;
   for(i=0;i<n;i++)
   {
   taxArray[i][0]=0;
    taxArray[i][0]=amount[i]*cityTaxRate/100;
    taxArray[i][1]=0;
    taxArray[i][1]=amount[i]*countryTaxRate/100;
   taxArray[i][2]=0;   
if(amount[i]>1000){
taxArray[i][2]=amount[i]*stateTaxRate/100;
   }
   }
   return    taxArray;
}
int main()
{
   Problem2C pbm2;
   ofstream outfile; //output file stream variable
ifstream infile; //input file stream variable
int n=0,i;
double** ansTaxArray;
infile.open("dollars.txt");// create this file in proper location with data
outfile.open("taxNew.txt");
do
{
   infile>>pbm2.amount[n++];
}while(!infile.eof());
ansTaxArray=pbm2.calculateTaxes(n);
outfile<<endl<<endl<<"TAX CALCULATION";
   outfile<<endl<<"===================================================================";
outfile<<endl<<"Amount in $"<<" "<<"3 %Tax"<<" "<<"4 %Tax"<<" "<<"1 %Tax"<<" ";
   for(i=0;i<n;i++)
{

   outfile<<endl<<pbm2.amount[i]<<" "<<*(ansTaxArray+i+0)<<" "<<*(ansTaxArray+i+1)<<" "<<*(ansTaxArray+i+2);
}
outfile<<endl<<"====================================================================";
infile.close();
outfile.close();
   return 0;
}

Output:

TAX CALCULATION
===================================================================
Amount in $ 3 %Tax 4 %Tax 1 %Tax Total Tax
10 0.3 0.4 0 0.7
100 3 4 0 7
1000 30 40 0 70
2000 60 80 20 160
====================================================================