Tax Programme help

This is a discussion on Tax Programme help within the C Programming forums, part of the General Programming Boards category; Greetings I have been trying to write a programme to work out the tax paid on a person's income based ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    New Zealand
    Posts
    2

    Tax Programme help

    Greetings

    I have been trying to write a programme to work out the tax paid on a person's income based on the different tax rates for different incomes. I keep getting an output value larger than the input value. Any help on this would be greatly appreciated.

    Code:
    #include <stdio.h>
    
    // Prints out the tax that is paid from a given income
    int main(){
               int income = 13000; // person's income before tax
               float tax_rate_1 = 0.105;
               float tax_rate_2 = 0.175;
    	   float tax_rate_3 = 0.30;
    	   float tax_rate_4 = 0.33;
    	   int income_left;
    	   int income_left2;
    	   int total_tax1 = 0, total_tax2 = 0, total_tax3 = 0, total_tax4 = 0;
    	   int income_tax;
    		
    
    	   if (income <= 14000) // for income less than $14000
    		  	total_tax1 = income * tax_rate_1;
    	
               if (income <= 48000 && income > 14000)
            	        income_left = income - 14000;
    			total_tax2 = income_left * tax_rate_2;
    			
               if (income <= 70000 && income > 48000)
    			income_left2 = income - 48000;
    			total_tax3 = income_left2 * tax_rate_3;
    			
    	    if (income > 70000);
             	        total_tax4 = (income - 70000) * tax_rate_4;
             	        
    	   income_tax = total_tax1 + total_tax2 + total_tax3 + total_tax4;
    	   printf("Total tax paid is $%d\n", income_tax);
    
           }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,690
    Here lies the main problem at hand:

    Code:
    if (income > 70000);
        total_tax4 = (income - 70000) * tax_rate_4;
    The "if()" statement is followed by an null statement (just ';' and perfectly legal in 'C'), so the next line is always getting executed. With your seed value of "income," this will yield a negative result.

    ~

    I would also like to add caution! You're using floats for your tax rates, but storing them, adding them, and printing them as integers!! Change those variables to type float and the format specifier in your "printf()" to "%f".
    Last edited by Matticus; 07-19-2011 at 07:33 PM.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You forgot brackets for your compound if statements and your last if statement contains a ';' which shouldn't be there.
    e.g.
    Code:
    if(something){
         ....do something
    }
    Additionally, you declare main as returning an int, however you do not in this program. Bare bones C program is:
    Code:
    int main(void){
    
         return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    New Zealand
    Posts
    2
    Ah, a semicolon where there should not be one!. I have fixed up the errors and it works now. Thanks for the assistance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about this programme
    By kasun007 in forum C Programming
    Replies: 1
    Last Post: 03-07-2011, 08:52 PM
  2. programme
    By muraja in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2010, 01:44 PM
  3. help me with ma c programme..
    By eddie19 in forum C Programming
    Replies: 19
    Last Post: 08-12-2009, 08:53 AM
  4. help me with this programme
    By ChrisWang in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2008, 03:21 AM
  5. Need help with this programme
    By korede in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2007, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21