Hi all. thx for reading!
I am having a problem writing a tax program that is quite simple but for some reason i cant get the keyboard inputs to work correctly. I have tried interchanging getchar for scanf, using while loops to try and eat excess characters in the input buffer (using both scanf and getchar) and even simply adding extra getchar's around the input functions to try and eat anything extra.
What happens is, when I get to an input line, it skips past it directly to the next one. I would say that it appears to be on the third function call for character input, rotating whenever I add a while loop before one of the input calls to eat up extra characters. I tried adding while loops to each function to no avail, the pattern breaks.
The prog might have some other inneffectual errors, like printing something to the screen before it should, but they shouldnt have an effect on the input problem(?). Hard to debug when I seem to identify the main bug!
Any suggestions would be greatly appreciated. Thanks all.
Code:
#include <stdio.h>
#include <ctype.h>
int main(void){
int terminate2 = 0, totalwages, taxes2pay;
char status, mstatus, terminate;
while(terminate != 'n' || terminate != 'N'){
printf("\nPlease enter your tax filing catagory. Options are:\n\nSingle\nHead of Household\nMarried (Joint or Separate)\n\n:");
scanf("%c", &status);
status = toupper(status);
printf("Enter wages earned last tax year: ");
scanf("%d", &totalwages);
switch (status){
case 'S' :
if (totalwages > 17850.00){
taxes2pay = (0.15 * 17850.00) + (0.28 * (totalwages - 17850.00));
}
else {
taxes2pay = (0.15 * totalwages);
}
break;
case 'H' :
if (totalwages > 17850.00){
taxes2pay = (0.15 * 23900.00) + (0.28 * (totalwages - 17850.00));
}
else {
taxes2pay = (0.15 * totalwages);
}
break;
case 'M':
while(terminate2 != 1){
printf("Are you filing Joint or Separate?\n\n:");
scanf("%c", &mstatus);
toupper(mstatus);
if (mstatus == 'J'){
if (totalwages > 17850.00){
taxes2pay = (0.15 * 29750.00) + (0.28 * (totalwages - 29750.00));
terminate2++;
}
else {
taxes2pay = (0.15 * totalwages);
terminate2++;
}
}
else if (mstatus == 'S'){
if (totalwages > 17850.00){
taxes2pay = (0.15 * 14875.00) + (0.28 * (totalwages - 14875.00));
terminate2++;
}
else {
taxes2pay = (0.15 * totalwages);
terminate2++;
}
}
else
printf("Please type either Joint or Separate.");
}
default :
printf("Please enter either Single, Head of Household, or Married.");
}
printf("\n\nTotal taxes: $%d\n\n", taxes2pay);
// while(getchar() != '\n'); //trying to eat up extra chars in the input buffer
printf("Would you like to perform another calculation? (Y/N): ");
scanf("%c", &terminate);
}
system("pause");
return 0;
}