Dear mandragora20,
Here's a fully commented C program for you to study.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
/* the above are required headers for standard libraries */
int main() {
char entry[1000], code; /* entry is a character buffer for input */
double income, threshold, tax; /* needed for calculations */
printf("=== TAX CALCULATOR ===\n"); /* title */
while (1) { /* infinite loop: true until we break out of it */
printf("--- tax categories ---\n"); /* display the codes */
printf("s = single\n");
printf("h = head of household\n");
printf("m = married-joint\n");
printf("n = married-separate\n");
printf("\n");
printf("Enter one of the above codes, or anything else to quit >");
fgets(entry, 1000, stdin); /* read a line of input */
if (strlen(entry) != 2) /* if it's not one character plus... */
break; /* ... a newline character, we quit */
code = entry[0]; /* the code is the first character */
if (!(code == 's' || code == 'S' || code == 'h' || code == 'H'
|| code == 'm' || code == 'M' || code == 'n' || code == 'N'))
break; /* if it's not a legal code, we quit */
printf("Enter your income in dollars, without a comma >");
fgets(entry, 1000, stdin); /* read another line */
income = atof(entry); /* convert into a floating-point number */
if (code == 's' || code == 'S') /* check the code (uppercase, too) */
threshold = 17850.0; /* set the threshold */
else if (code == 'h' || code == 'H') /* otherwise, check another code */
threshold = 23900.0; /* appropriate threshold here */
else if (code == 'm' || code == 'N') /* same thing again */
threshold = 29750.0;
else /* if no other code matches, it must be 'n' or 'N' */
threshold = 14875.0;
if (income < threshold) /* are we under the threshold? */
tax = 0.15*income; /* then just apply the base rate */
else /* otherwise, apply the excess rate, too */
tax = 0.15*threshold + 0.28*(income-threshold);
printf("-----------> You owe $%.2f in tax.\n\n", tax);
} /* while loop returns to beginning */
printf("\nGoodbye!\n"); /* if we broke out of the loop, we land here */
return 0; /* always return zero at the end of main() */
}
If you have any trouble compiling this program, please let me know.
Any questions that aren't answered by the comments, don't hesitate to
ask.
At your service,
leapinglizard |