C program for calculating tax brackets(no arrays)

  • Thread starter Thread starter HoojaMan33
  • Start date Start date
  • Tags Tags
    Arrays Program
AI Thread Summary
The discussion focuses on a C programming assignment that requires creating functions to identify tax brackets, calculate taxes, and print results without using arrays. Key issues include an undefined reference error for the `printAll` function, which may stem from incorrect function calls or extra braces in the code. The code also had bugs related to handling income thresholds and uninitialized variables, leading to incorrect tax bracket and tax calculations. Suggestions include using loops to handle multiple income inputs and ensuring proper function calls within the `main` function. The conversation emphasizes debugging techniques and proper variable management in C programming.
HoojaMan33
Messages
15
Reaction score
0

Homework Statement


I have an assignment with the following requirements for the functions:
  1. Tax_Bracket_Identification. This function will identify the tax bracket. This function:
    • is of type int;
    • has one call-by-value argument (income);
    • uses nested if-else statements.
  2. Tax_Calculation. Will calculate the tax for each income scanned and:
    • is of type void;
    • has three arguments:
      • the income (call-by-value);
      • the tax bracket (call-by-value);
      • the tax (call-by-reference);
    • uses a switch case statement to calculate the income tax.
  3. PrintAll. This function will print the tax bracket and income tax. It:
    • is of type void;
    • has two arguments:
      • the tax bracket (call-by-value);
      • the income tax (call-by-value).

Homework Equations

The Attempt at a Solution


Code:
#include <stdio.h>
int taxBracketIdentification(int);
void taxCalculation(int income, int taxbracket, int *tax);
void printAll(int taxbracket, int tax);
int taxbracket;

void main() {
int  incomenumber, tax, income;

printf("Please type in the number of incomes to be processed. \n");
scanf("%d", &incomenumber);
printf("Please type in the income \n");
scanf("%d", &income);

printAll(tax, taxbracket);
}

int taxBracketIdentification(int income) {
int taxbracket;

if (income < 10000) {
  taxbracket = 1;
}

  else if (income < 20000)
  {
  taxbracket = 2;
  }

  else if (income <30000)
  {
  taxbracket = 3;
  }
  else if (income < 50000)
  {
  taxbracket = 4;

  }
  else if (income < 100000)

  {
  taxbracket = 5;
  }

  else if (income > 100000)
  {
  taxbracket = 6;
  }

}

void taxCalculation(int income, int taxbracket, int *tax) {

(taxBracketIdentification(income));
switch(taxbracket) {
case 1:
  *tax = (0.05 * income);
  break;
case 2:
  *tax = 500 + 0.10*(income - 10000);
  break;
case 3:
  *tax = 1500 + 0.15*(income-20000);
  break;
case 4:
  *tax = 3000 + 0.20*(income-30000);
  break;
case 5:
  *tax = 7000 + 0.25*(income-50000);
  break;
case 6:
  *tax = 19500  + 0.30*(income-10000);
  break;

}

void printAll(int taxbracket, int tax)
{

  printf("Your tax bracket is: \t %d \n", taxbracket);
  printf("Your tax is \t %d \n", tax);}
}

Why is it telling me that there is an undefined reference to printAll?
 
Physics news on Phys.org
HoojaMan33 said:

Homework Statement


I have an assignment with the following requirements for the functions:
  1. Tax_Bracket_Identification. This function will identify the tax bracket. This function:
    • is of type int;
    • has one call-by-value argument (income);
    • uses nested if-else statements.
  2. Tax_Calculation. Will calculate the tax for each income scanned and:
    • is of type void;
    • has three arguments:
      • the income (call-by-value);
      • the tax bracket (call-by-value);
      • the tax (call-by-reference);
    • uses a switch case statement to calculate the income tax.
  3. PrintAll. This function will print the tax bracket and income tax. It:
    • is of type void;
    • has two arguments:
      • the tax bracket (call-by-value);
      • the income tax (call-by-value).

Homework Equations

If you're showing C code, use
C:
 instead of just plain [code] for the opening tag.

Also, the code below doesn't have any indentation, which makes it hard to spot obvious errors.
[QUOTE=HoojaMan33]
[h2]The Attempt at a Solution[/h2]
[code]
#include <stdio.h>
int taxBracketIdentification(int);
void taxCalculation(int income, int taxbracket, int *tax);
void printAll(int taxbracket, int tax);
int taxbracket;

void main() {
int  incomenumber, tax, income;

printf("Please type in the number of incomes to be processed. \n");
scanf("%d", &incomenumber);
printf("Please type in the income \n");
scanf("%d", &income);

printAll(tax, taxbracket);
}

int taxBracketIdentification(int income) {
int taxbracket;

if (income < 10000) {
  taxbracket = 1;
}

  else if (income < 20000)
  {
  taxbracket = 2;
  }

  else if (income <30000)
  {
  taxbracket = 3;
  }
  else if (income < 50000)
  {
  taxbracket = 4;

  }
  else if (income < 100000)

  {
  taxbracket = 5;
  }

  else if (income > 100000)
  {
  taxbracket = 6;
  }

}

void taxCalculation(int income, int taxbracket, int *tax) {

(taxBracketIdentification(income));
switch(taxbracket) {
case 1:
  *tax = (0.05 * income);
  break;
case 2:
  *tax = 500 + 0.10*(income - 10000);
  break;
case 3:
  *tax = 1500 + 0.15*(income-20000);
  break;
case 4:
  *tax = 3000 + 0.20*(income-30000);
  break;
case 5:
  *tax = 7000 + 0.25*(income-50000);
  break;
case 6:
  *tax = 19500  + 0.30*(income-10000);
  break;

}

void printAll(int taxbracket, int tax)
{

  printf("Your tax bracket is: \t %d \n", taxbracket);
  printf("Your tax is \t %d \n", tax);}
}

Why is it telling me that there is an undefined reference to printAll?[/QUOTE]
It looks to me like you have an extra right brace - }. That could be throwing the compiler off, resulting in the error you see. If that's not it, please give us the exact error message, which will have a line number.

Also, I see a bug in your code. What happens in the income is $100,000?
 
Okay i changed it to income >= 100000 over 10k, that bug shouldn't occur anymore.

It compuiles fine but now it is telling me my tax bracket is 39 and my tax is 0 lol... what is going on here?
 
and how do i get multiple inputs from the user? (say up to 3) without using arrays?
 
HoojaMan33 said:
Okay i changed it to income >= 100000 over 10k, that bug shouldn't occur anymore.

It compuiles fine but now it is telling me my tax bracket is 39 and my tax is 0 lol... what is going on here?
Your main() function needs work. In it, you read incomenumber and income from the user, and then call printAll(), passing the uninitialized (hence garbage) tax value and taxbracket, a global, so is initialized to 0. printAll() merely displays a garbage value (tax) and 0 for taxbracket.

1. Why is taxbracket a global variable?
2. Why aren't you calling taxBracketIdentification() and taxCalculation()?
 
HoojaMan33 said:
and how do i get multiple inputs from the user? (say up to 3) without using arrays?
Use a loop.
 

Similar threads

Replies
4
Views
1K
Replies
3
Views
1K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
3
Views
1K
Replies
3
Views
2K
Back
Top