1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

unable to understand the error in the following code

Discussion in 'C' started by bsudhir6, Jan 9, 2011.

  1. bsudhir6

    bsudhir6 New Member

    Joined:
    Apr 4, 2010
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    /* Calculate the tax due or the refund for family based on the following imaginary formula
         1. For each dependent deduct $1,000 from income,
         2.Determine tax rte from the following brackets;
         
         bracket             taxable income                tax rate
                 1                                <=10000                            2%
                 2                    10001 - 20000                            5%
                 3                    30001 - 40000                            7%
                 4                    40001 - 50000                            10%
                 2                    50001 and up                            15%
         then print the amount of tax or the refund        
         @author:Sudhir
         @Date and Time: 1/9/2011 2:16:25 PM
    */
    
    #include<stdio.h>
    
    #define LOWEST     0000000.00
    #define HIGHEST 1000000.00
    
    #define LIMIT1 10000.00
    #define LIMIT2 20000.00
    #define LIMIT3 30000.00
    #define LIMIT4 40000.00
    #define LIMIT5 50000.00
    
    #define RATE1 02
    #define RATE2 05
    #define RATE3 07
    #define RATE4 10
    #define RATE5 15
    
    #define DEDN_PER_DPNDNT 1000
    
    //Function Declarations
    void getData(double* totalIncome, double* taxPaid, int* numOfDpndnts);
    void calcTaxes(double totalIncome, double taxPaid, int numOfDpndnts, double* taxableIncome, double* totalTax, double* taxDue);
    void printInformation(double totalIncome, double taxPaid, int numOfDpndnts, double totalTax, double paidTax,double taxDue);
    double bracketTax(double income, double startLimit, double stopLimit, int rate);
    
    int main(int argc,char *argv[])
    {
        //Local Declarations
        int numOfDpndnts;
        double taxDue;
        double taxPaid;
        double totalIncome;
        double taxableIncome;
        double totalTax;
        
        //Statements
        getData(&totalIncome, &taxPaid, &numOfDpndnts);
        calcTaxes(totalIncome, taxPaid, numOfDpndnts, &taxableIncome, &totalTax, &taxDue);
        printInformation(totalIncome, taxableIncome, numOfDpndnts, totalTax, taxPaid, taxDue);
        
        return 0;
    }//main
    
    /* ==============================getData()===============================
         This function reads tax data from the keyboard.
         
             pre: nothing
             post: Reads totalIncome, taxPaid, &numOfDpndnts
    */
    void getData(double* totalIncome, double* taxPaid, int* numOfDpndnts)
    {
        //Statements
        printf("Enter your total income for the year: \n");
        scanf("%lf", totalIncome);
        
        printf("Enter total of payroll deductions: \n");
        scanf("%lf", taxPaid);
        
        printf("Enter the number of dependents: \n");
        scanf("%lf", numOfDpndnts);
        
        return;
    }//getData
        
    /* ==============================calcTaxes()===============================
         This function calculates the tax due.
         
             pre: Given-Income, numOfDpndnts
             post: Tax Income, total Tax, and tax Due calculated
    */
    void calcTaxes(double totalIncome, double taxPaid, int numOfDpndnts, double* taxableIncome, double* totalTax, double* taxDue)
    {
        //Statements
    [COLOR=Red]    taxableIncome = totalIncome - (numOfDpndnts * DEDN_PER_DPNDNT);[/COLOR]
        *totalTax = bracketTax(*taxableIncome, LOWEST, LIMIT1, RATE1) + bracketTax(*taxableIncome, LIMIT1, LIMIT2, RATE2) + bracketTax(*taxableIncome, LIMIT2, LIMIT3, RATE3) + bracketTax(*taxableIncome, LIMIT3, LIMIT4, RATE4) + bracketTax(*taxableIncome, LIMIT4, LIMIT5, RATE5) ;
        *taxDue = *totalTax - taxPaid;
        return;
    }//calcTaxes
    
    /* ==============================printInformation()===============================
         This function prints a table showing all information
         
             pre: The parameter list
             post: Prints the table
    */
    
    void printInformation(double totalIncome, double taxPaid, int numOfDpndnts, double totalTax, double paidTax,double taxDue)
    {
        //Statements
        printf("\n Total Income:                                                        %10.2f\n", totalIncome);
        printf("\n Number of dependents:                                        %7d\n", numOfDpndnts);
        printf("\n Taxable Income:                                                    %10.2f\n", taxPaid);
        printf("\n Total Tax                                                                %10.2f\n", totalTax);
        printf("\n Tax Already Paid:                                                %10.2f\n", paidTax);
        if(taxDue >= 0.0)
                printf("Tax due:                                                                %10.2f\n", taxDue);
        else 
            printf("refund:                                                                %10.2f\n", -taxDue);
        
        return;
    }//printInformation    
        
    /* ==============================bracketTax()===============================
         This function calculates the tax for particular bracket
         
             pre: The taxable income
             post: Return the tax for particular bracket
    */
    
    double bracketTax(double income, double startLimit, double stopLimit, int rate)
    {
        //Local Declarations
        double tax;
        
        //Statements
        if(income <= startLimit)
            tax = 0.0;
        else
            if( income > startLimit && income <= stopLimit)
                tax = (income > stopLimit) * rate /100.00;
            else
                tax = (stopLimit - startLimit) * rate / 100.00;
        return tax;
    }// bracketTax        
    
    error in visual c++ compiler is
    Code:
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    Tax.c
    Tax.c(69) : warning C4996: 'scanf' was declared deprecated
            C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(295) : see
     declaration of 'scanf'
            Message: 'This function or variable may be unsafe. Consider using scanf_
    s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help
     for details.'
    Tax.c(72) : warning C4996: 'scanf' was declared deprecated
            C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(295) : see
     declaration of 'scanf'
            Message: 'This function or variable may be unsafe. Consider using scanf_
    s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help
     for details.'
    Tax.c(75) : warning C4996: 'scanf' was declared deprecated
            C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(295) : see
     declaration of 'scanf'
            Message: 'This function or variable may be unsafe. Consider using scanf_
    s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help
     for details.'
    Tax.c(89) : error C2440: '=' : cannot convert from 'double' to 'double *'
    
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    For the warning messages you can either use the non-standard function Microsoft recommends scanf_
    s in this case or #define _CRT_SECURE_NO_DEPRECATE . Please see this link Security enhancements.

    For the error:

    Code:
    void calcTaxes(double totalIncome, double taxPaid, int numOfDpndnts, double* taxableIncome, double* totalTax, double* taxDue)
    {
        //Statements
        taxableIncome = totalIncome - (numOfDpndnts * DEDN_PER_DPNDNT); // Line 89
    
    Your variable taxableIncome is a pointer to a double and you are trying to assign a double to it. You must add the * in front of taxableIncome.

    Also I get a warning on line 75.

    And line 75 is:
    Code:
       scanf("%lf", numOfDpndnts);
    
    And numOfDpndnts is an int* not a floating point number. So the format specifier should be "%d".

    This warning should also be found by your compiler, but I guess Microsoft is more concerned about "Significant enhancements" that they forget about the real problems.

    Jim
     
    shabbir likes this.
  3. lincy

    lincy New Member

    Joined:
    Jan 7, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Code:
    #include<stdio.h>
    
    #define LOWEST     0000000.00
    #define HIGHEST 1000000.00
    
    #define LIMIT1 10000.00
    #define LIMIT2 20000.00
    #define LIMIT3 30000.00
    #define LIMIT4 40000.00
    #define LIMIT5 50000.00
    
    #define RATE1 02
    #define RATE2 05
    #define RATE3 07
    #define RATE4 10
    #define RATE5 15
    
    #define DEDN_PER_DPNDNT 1000
    
    //Function Declarations
    
    void getData(double totalIncome, double taxPaid, int numOfDpndnts);
    void calcTaxes(double totalIncome, double taxPaid, int numOfDpndnts, double taxableIncome, double totalTax, double taxDue);
    void printInformation(double totalIncome, double taxPaid, int numOfDpndnts, double totalTax, double paidTax,double taxDue);
    double bracketTax(double income, double startLimit, double stopLimit, int rate);
    
    void main(int argc,char argv[])
    {
        //Local Declarations
        int numOfDpndnts;
        double taxDue;
        double taxPaid;
        double totalIncome;
        double taxableIncome;
        double totalTax;
        
        //Statements
    
        getData(&totalIncome, &taxPaid, &numOfDpndnts);
        calcTaxes(totalIncome, taxPaid, numOfDpndnts, &taxableIncome, &totalTax, &taxDue);
        printInformation(totalIncome, taxableIncome, numOfDpndnts, totalTax, taxPaid, taxDue);
        
       
    }//main
    
    /* ==============================getData()===============================
         This function reads tax data from the keyboard.
         
             pre: nothing
             post: Reads totalIncome, taxPaid, &numOfDpndnts
    */
    
    void getData(double totalIncome, double taxPaid, int numOfDpndnts)
    {
        //Statements
    
        printf("Enter your total income for the year: \n");
        scanf("%lf", totalIncome);
        
        printf("Enter total of payroll deductions: \n");
        scanf("%lf", taxPaid);
        
        printf("Enter the number of dependents: \n");
        scanf("%lf", numOfDpndnts);
        
        return;
    } //getData
        
    /* ==============================calcTaxes()===============================
         This function calculates the tax due.
         
             pre: Given-Income, numOfDpndnts
             post: Tax Income, total Tax, and tax Due calculated
    */
    
    void calcTaxes(double totalIncome, double taxPaid, int numOfDpndnts, double taxableIncome, double totalTax, double taxDue)
    {
        //Statements
    
        taxableIncome = totalIncome - (numOfDpndnts * DEDN_PER_DPNDNT);
        totalTax = bracketTax(taxableIncome, LOWEST, LIMIT1, RATE1) + bracketTax(*taxableIncome, LIMIT1, LIMIT2, RATE2) + bracketTax(taxableIncome, LIMIT2, LIMIT3, RATE3) + bracketTax(taxableIncome, LIMIT3, LIMIT4, RATE4) + bracketTax(taxableIncome, LIMIT4, LIMIT5, RATE5) ;
        taxDue = totalTax - taxPaid;
        return;
    }//calcTaxes
    
    /* ==============================printInformation()===============================
         This function prints a table showing all information
         
             pre: The parameter list
             post: Prints the table
    */
    
    void printInformation(double totalIncome, double taxPaid, int numOfDpndnts, double totalTax, double paidTax,double taxDue)
    {
        //Statements
    
        printf("\n Total Income:%10.2f\n", totalIncome);
        printf("\n Number of dependents:%7d\n", numOfDpndnts);
        printf("\n Taxable Income:%10.2f\n", taxPaid);
        printf("\n Total Tax %10.2f\n", totalTax);
        printf("\n Tax Already Paid:%10.2f\n", paidTax);
        if(taxDue >= 0.0)
                printf("Tax due:%10.2f\n", taxDue);
        else 
            printf("refund:%10.2f\n", -taxDue);
        
        return;
    }//printInformation    
        
    /* ==============================bracketTax()===============================
         This function calculates the tax for particular bracket
         
             pre: The taxable income
             post: Return the tax for particular bracket
    */
    
    
    void double bracketTax(double income, double startLimit, double stopLimit, int rate)
    {
        //Local Declarations
        double tax;
        
        //Statements
        if(income <= startLimit)
            tax = 0.0;
        else
            if( income > startLimit && income <= stopLimit)
                tax = (income > stopLimit) * rate /100.00;
            else
                tax = (stopLimit - startLimit) * rate / 100.00;
        return (0);
    }// bracketTax
    
    not sure about program ,just try
     
    Last edited by a moderator: Jan 10, 2011

Share This Page