C program for calculating tax brackets(no arrays)

  • Thread starter Thread starter HoojaMan33
  • Start date Start date
  • Tags Tags
    Arrays Program
Click For Summary

Discussion Overview

The discussion revolves around a C programming assignment focused on calculating tax brackets without using arrays. Participants are addressing specific requirements for functions related to tax bracket identification, tax calculation, and printing results. The conversation includes code snippets, debugging issues, and questions about handling multiple inputs.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant outlines the requirements for three functions: Tax_Bracket_Identification, Tax_Calculation, and PrintAll, detailing their expected types and arguments.
  • Another participant points out the lack of indentation in the provided code, suggesting it makes it difficult to identify errors.
  • Concerns are raised about an "undefined reference" error related to the PrintAll function, with one participant suggesting that an extra right brace might be causing the issue.
  • Participants discuss a bug where the tax bracket is incorrectly calculated as 39, with one participant noting that the main function needs adjustments to properly initialize variables and call necessary functions.
  • There is a question about how to handle multiple user inputs without using arrays, with a suggestion to use a loop.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the code's functionality and errors. There is no consensus on the resolution of the undefined reference error or the incorrect tax bracket calculation, as multiple issues are identified without a clear solution being agreed upon.

Contextual Notes

Participants mention potential bugs in the code, such as uninitialized variables and the use of a global variable for taxbracket, which may lead to unexpected behavior. The discussion also highlights the need for proper function calls to ensure correct calculations.

Who May Find This Useful

Students learning C programming, particularly those working on assignments involving functions and tax calculations, may find this discussion relevant.

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 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
9
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K