Hey guys, have a question with a Tax program using strings, my calculations are
ID: 3690494 • Letter: H
Question
Hey guys, have a question with a Tax program using strings, my calculations are incorrect when I compile the program. when it compiles it says the taxable income is the same as the deduction, which throws off the tax rate, taxes payable, and refund due.. I've tried a couple different variations and still not getting the correct output when I compile. Any suggestions, below you'll find my code, thanks!
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
string convertletters(string);
double cal_taxable_income(double, int);
double cal_deduction(string);
int cal_tax_rate(double, string);
int main()
{
string name, status, deduction, temp, s;
int no_depen, i, tax_rate;
double gross_income, tax_withheld, taxable_income, tax;
while(cin>>name)
{
taxable_income=0.0;
tax_rate=0;
tax=0.0;
name=convertletters(name);
cout << "Tax summary for "+ name+" family" <<endl;
cin>>status;
status=convertletters(status);
cout << "Filing status: " +status << endl;
cin >> no_depen;
string dependents [no_depen];
for (i=0;i<no_depen;i++)
{
cin >> dependents[i];
dependents[i]=convertletters(dependents[i]);
}
cout << "Dependents: " ;
for(i=0;i<no_depen;i++)
{
if(i==no_depen-1)
cout << dependents[i] << endl;
else
cout << dependents[i]+" , " ;
}
cin >> gross_income;
cout << "Gross income: " << setw(9) << " $ " << setw(9) << setprecision(2) << fixed << gross_income << endl;
cin >> tax_withheld;
taxable_income=cal_taxable_income(gross_income, no_depen);
cout << "Exemptions: " << setw(11) << " - " << setw(9) << setprecision(2) << fixed << 1200.0*no_depen << endl;
cin >> deduction;
deduction=convertletters(deduction);
double deduction_amount = 0.0;
deduction_amount=cal_deduction(deduction);
taxable_income=deduction_amount;
if(taxable_income<0)
taxable_income=0.0;
cout << "Taxable Income: " << setw(16) << setprecision(2) << fixed << taxable_income << endl;
tax_rate=cal_tax_rate(taxable_income,status);
cout << "Tax rate: " << setw(22) << tax_rate << "%" << endl;
tax=(taxable_income*tax_rate)/100.00;
cout << "Taxes payable: " << setw(17) << setprecision(2) << fixed << tax << endl;
cout << "Taxes withheld: " << setw(7) << " - " << setprecision(2) << fixed << setw(9) << tax_withheld << endl;
tax=tax-tax_withheld;
tax=double(tax);
if(tax>0)
cout << "Taxes due: " << setw(12) << " $ " << setprecision(2) << fixed << setw(9) << tax << endl;
else
cout << "Refund due: " << setw(11) << " $ " << setprecision(2) << fixed << setw(9) << (-tax) << endl;
cout << endl;
}
return 0;
}
string convertletters(string s)
{
s[0] =char(toupper(s[0]));
for (inti=1;i<s.length();i++)
{
s[i]=char(tolower(s[i]));
}
return s;
}
double cal_taxable_income(double gross_income, int no_depen)
{
return gross_income-1200*no_depen;
}
double cal_deduction(string deduction)
{
double deduction_amount=0.0;
if(deduction.compare("Standard" ) ==0)
{
deduction_amount=3000.00;
cout << "Standard deduction: - " << setw(9) << 3000.00 << endl;
}
else if(deduction.compare("Itemized" ) ==0)
{
double item_deduction=0.0, temp;
cin >> temp;
while(temp!=0)
{
item_deduction+=temp;
cin >> temp;
}
if (item_deduction>=3000)
{
deduction_amount=item_deduction;
cout << "Itemized deduction: - " << setprecision(2) << fixed << setw(9) << item_deduction << endl;
}
else
{
deduction_amount=3000.00;
cout << "Standard deduction: - " << setw(9) << 3000.00 << endl;
}
}
return deduction_amount;
}
int cal_tax_rate(double taxable_income, string status)
{
int tax_rate;
if(taxable_income<30000)
{ {
if(status.compare("Single") ==0)
tax_rate=4;
else if(status.compare("Married") ==0)
tax_rate=2;
}
else if(taxable_income>=30000 && taxable_income<100000)
{
if(status.compare("Single")==0)
tax_rate=20;
else if(status.compare("Married")==0)
tax_rate=18;
}
else
{
if(status.compare("Single")==0)
tax_rate=35;
else if(status.compare("Married")==0)
tax_rate=30;
}
return tax_rate;
}
Explanation / Answer
Hi, there was silly mistake. I have corrected it. Please test now.
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
string convertletters(string);
double cal_taxable_income(double, int);
double cal_deduction(string);
int cal_tax_rate(double, string);
int main()
{
string name, status, deduction, temp, s;
int no_depen, i, tax_rate;
double gross_income, tax_withheld, taxable_income, tax;
while(cin>>name)
{
taxable_income=0.0;
tax_rate=0;
tax=0.0;
name=convertletters(name);
cout << "Tax summary for "+ name+" family" <<endl;
cin>>status;
status=convertletters(status);
cout << "Filing status: " <<status << endl;
cin >> no_depen;
string dependents[no_depen];
for (i=0;i<no_depen;i++)
{
cin >> dependents[i];
dependents[i]=convertletters(dependents[i]);
}
cout << "Dependents: " ;
for(i=0;i<no_depen;i++)
{
if(i==no_depen-1)
cout << dependents[i] << endl;
else
cout << dependents[i]+" , " ;
}
cin >> gross_income;
cout << "Gross income: " << setw(9) << " $ " << setw(9) << setprecision(2) << fixed << gross_income << endl;
cin >> tax_withheld;
taxable_income=cal_taxable_income(gross_income, no_depen);
cout << "Exemptions: " << setw(11) << " - " << setw(9) << setprecision(2) << fixed << 1200.0*no_depen << endl;
cin >> deduction;
deduction=convertletters(deduction);
double deduction_amount = 0.0;
deduction_amount=cal_deduction(deduction);
taxable_income = taxable_income - deduction_amount;
if(taxable_income<0)
taxable_income=0.0;
cout << "Taxable Income: " << setw(16) << setprecision(2) << fixed << taxable_income << endl;
tax_rate=cal_tax_rate(taxable_income,status);
cout << "Tax rate: " << setw(22) << tax_rate << "%" << endl;
tax=(taxable_income*tax_rate)/100.00;
cout << "Taxes payable: " << setw(17) << setprecision(2) << fixed << tax << endl;
cout << "Taxes withheld: " << setw(7) << " - " << setprecision(2) << fixed << setw(9) << tax_withheld << endl;
tax=tax-tax_withheld;
tax=double(tax);
if(tax>0)
cout << "Taxes due: " << setw(12) << " $ " << setprecision(2) << fixed << setw(9) << tax << endl;
else
cout << "Refund due: " << setw(11) << " $ " << setprecision(2) << fixed << setw(9) << (-tax) << endl;
cout << endl;
}
return 0;
}
string convertletters(string s)
{
s[0] =char(toupper(s[0]));
for (unsigned int i=1;i<s.length();i++)
{
s[i]=char(tolower(s[i]));
}
return s;
}
double cal_taxable_income(double gross_income, int no_depen)
{
return gross_income - (1200*no_depen);
}
double cal_deduction(string deduction)
{
double deduction_amount=0.0;
if(deduction.compare("Standard") == 0)
{
deduction_amount=3000.00;
cout << "Standard deduction: - " << setw(9) << 3000.00 << endl;
}
else if(deduction.compare("Itemized" ) ==0)
{
double item_deduction=0.0, temp;
cin >> temp;
while(temp!=0)
{
item_deduction+=temp;
cin >> temp;
}
if (item_deduction>=3000)
{
deduction_amount=item_deduction;
cout << "Itemized deduction: - " << setprecision(2) << fixed << setw(9) << item_deduction << endl;
}
else
{
deduction_amount=3000.00;
cout << "Standard deduction: - " << setw(9) << 3000.00 << endl;
}
}
return deduction_amount;
}
int cal_tax_rate(double taxable_income, string status)
{
int tax_rate;
if(taxable_income<30000)
{
if(status.compare("Single") ==0)
tax_rate=4;
else if(status.compare("Married") ==0)
tax_rate=2;
}
else if(taxable_income>=30000 && taxable_income<100000)
{
if(status.compare("Single")==0)
tax_rate=20;
else if(status.compare("Married")==0)
tax_rate=18;
}
else
{
if(status.compare("Single")==0)
tax_rate=35;
else if(status.compare("Married")==0)
tax_rate=30;
}
return tax_rate;
}