this program will calculate your tax bracket by the entering your income and your marital status, s for single and m for married.
The program will prompt you to enter income, then marital status. However it does not calculate income tax owed, always displays $0.00. Any help would be appreciated. Thank you very much!
Here is my code so far:
int taxBracket (double income, char maritalStatus);
double calculateTax(double income, int taxBracket);
int main() {
double income;
char maritalStatus;
int taxBracketValue;
printf("Enter income: ");
scanf("%le", &income);
printf("Enter marital status (s for single, m for married): ");
scanf(" %c", &maritalStatus);
taxBracketValue = taxBracket(income, maritalStatus);
if(taxBracketValue == -1) {
printf("Invalid marital status.\n");
return 1;
}
double tax = calculateTax(income, taxBracketValue);
printf("Income tax is $%.2f\n", tax);
return 0;
}
int taxBracket(double income, char maritalStatus) {
if (maritalStatus == 's') {
if(income < 15000)
return 0;
else if (income >= 15000 && income < 50000)
return 1;
else if (income >= 50000 && income < 90000)
return 2;
else if (income >=90000 && income < 160000)
return 3;
else
return 4;
}
else if (maritalStatus == 'm') {
if (income < 30000)
return 0;
else if (income>= 30000 && income < 80000)
return 1;
else if (income>= 80000 && income < 140000)
return 2;
else if (income >= 140000 && income < 220000)
return 3;
else
return 4;
}
return -1;
}
double calculateTax(double income, int taXBracket) {
double tax = 0.0;
int taxBracket;
switch(taxBracket) {
case 0:
tax = 0.0;
break;
case 1:
tax = income * 0.10;
break;
case 2:
tax = income * 0.15;
break;
case 3:
tax = income * 0.23;
break;
case 4:
tax = income * 0.33;
break;
default:;
break;
}
return tax;
}
Code is hard to read when it's poorly formatted.
In calculateTax()
the local variable taxBracket
is uninitialized but you want to use the argument which is capitalized differently. This is your main issue, and your compiler should generate a warning for use of the uninitialized variable.
The include for scanf()
and printf()
functions is missing.
For small programs you can usually eliminate the prototypes by moving main()
to the bottom.
(Not fixed) Business rules like taxbracket()
and calculatetax()
are subject to frequent change. They benefit from being expressed as data (tables) instead of code possible externalized (file, database, web service etc). In this case it also eliminates all the income * ...
duplication. This return early-pattern for the special cases often lead to concise code.
double calculatetax(double income, int taxbracket) {
if(taxbracket < 1 || taxbracket > 4)
return 0;
const double rates[] = {0.1, 0.15, 0.23, 0.33};
return income * rates[taxbracket - 1];
}
You could refactor this, say, returning the rate, and let caller determine the tax amount by multiplying with income
:
double taxrate(int taxbracket) {
if(taxbracket < 1 || taxbracket > 4)
return 0;
const double rates[] = {0.1, 0.15, 0.23, 0.33};
return rates[taxbracket - 1];
}
Leave out the ;
in default:;
. The real power of a switch statements is realized when you switch on a enum
. If you leave out the catch-all default
the compiler can now tell if you are missing a case.
#include <stdio.h>
int taxbracket(double income, char maritalstatus) {
if (maritalstatus == 's') {
if(income < 15000)
return 0;
else if (income >= 15000 && income < 50000)
return 1;
else if (income >= 50000 && income < 90000)
return 2;
else if (income >=90000 && income < 160000)
return 3;
else
return 4;
}
else if (maritalstatus == 'm') {
if (income < 30000)
return 0;
else if (income>= 30000 && income < 80000)
return 1;
else if (income>= 80000 && income < 140000)
return 2;
else if (income >= 140000 && income < 220000)
return 3;
else
return 4;
}
return -1;
}
double calculatetax(double income, int taxbracket) {
double tax = 0.0;
switch(taxbracket) {
case 0:
tax = 0.0;
break;
case 1:
tax = income * 0.10;
break;
case 2:
tax = income * 0.15;
break;
case 3:
tax = income * 0.23;
break;
case 4:
tax = income * 0.33;
break;
default:
break;
}
return tax;
}
int main() {
double income;
char maritalstatus;
int taxbracketvalue;
printf("enter income: ");
scanf("%le", &income);
printf("enter marital status (s for single, m for married): ");
scanf(" %c", &maritalstatus);
taxbracketvalue = taxbracket(income, maritalstatus);
if(taxbracketvalue == -1) {
printf("invalid marital status.\n");
return 1;
}
double tax = calculatetax(income, taxbracketvalue);
printf("income tax is $%.2f\n", tax);
return 0;
}
and example run:
Enter income: 30000
Enter marital status (s for single, m for married): s
Income tax is $3000.00