tax calucaltion functions c++

Hubert Mostek 41 Reputation points
2021-11-23T22:47:09.273+00:00

am a beginner programmer and I was to ask a program which calculates income tax based on a salary. Then to change thecode and create below functions however I have no idea how to create them. these are:

float calculate_tax(float amount, float tax_rate);
float calculate_netpay(float amount, float tax);
float increment_salary(float amount, float increment_rate)
.Also I am trying to loop the program so once you enter y or n to continue the program will either calculate again or stop however when I use do while function program does not stop and it calculates all the time so it kind of does not recognize false argument.See below code I created in Visio: Any suggestions would be appreciated.

the code is below:

include<stdio.h>

include<conio.h>

int main()

{
    int employeeID = 0;
    float salary = 0;
    float taxtotal = 0;
    float netpaytotal = 0;
    char c = '-';
    char d[] = "Employee_Payroll";
    int j, k;
    char grade = 0;
    double inc1 = 0.025;
    double inc2 = 0.015;
    double inc3 = 0;
    double Y1tax;
    double Y2tax;
    double Y3tax;
    double Y4tax;
    double Y5tax;
    double netpay;
    double newpay;
    double newpay1;
    double newpay2;
    double newpay3;
    double newpay4;
    char inputChar = 'y';

    do
    {
        printf("Enter Employee ID: ");
        scanf_s("%d", &employeeID);

        printf("\n");

        if (employeeID == 1)

        {
            printf("Enter Salary: ");
            scanf_s("%f", &salary);
        }
        else

        {
            printf("Wrong Employee ID\n");
            exit(0);
        }


        printf("\n");

        if (salary >= 12000 && salary <= 15000)
        {
            for (j = 1; j <= 1; j++)
            {
                for (k = 1; k <= 34; k++)
                {
                    printf("%c", c);
                }
                printf("\n");
                {
                    printf("-------Employee Payroll-----------\n");
                }
            }
            printf("\n");


            printf("Employee ID: %d\n", employeeID);
            printf("Gross Salary: %f\n", salary);
            taxtotal = salary * 0.25;
            printf("Tax: %f\n", taxtotal);

            netpaytotal = salary - taxtotal;
            printf("Net Pay: %f\n", netpaytotal);
        }
        else
        {
            printf("Error: Salary %f is out of range!!!\n", salary);
            exit(0);
        }

        printf("\n");


        if (salary >= 12000 && salary <= 15000 && employeeID == 1)

            for (j = 1; j <= 1; j++)
            {
                for (k = 1; k <= 34; k++)
                {
                    printf("%c", c);
                }
            }

        printf("\n");

        {
            printf("Enter Grade: %c", grade);
            "\n";
            scanf_s(" %c", &grade, 1);
        }
        printf("\n");

        switch (grade)

        {
        case 'A':
            printf("The increment is: %f\n", inc1);
            printf("Increment and tax over the five years\n");

            newpay = (salary * 0.025) + salary;

            Y1tax = newpay * 0.25;
            netpay = newpay - Y1tax;

            printf("Year 1: tax = %f, net pay = %f\n", Y1tax, netpay);

            newpay1 = (newpay * 0.025) + newpay;
            Y2tax = newpay1 * 0.25;
            netpay = newpay1 - Y2tax;

            printf("Year 2: tax = %f, net pay = %f\n", Y2tax, netpay);


            newpay2 = (newpay1 * 0.025) + newpay1;
            Y3tax = newpay2 * 0.25;
            netpay = newpay2 - Y3tax;

            printf("Year 3: tax = %f, net pay = %f\n", Y3tax, netpay);

            newpay3 = (newpay2 * 0.025) + newpay2;
            Y4tax = newpay3 * 0.25;
            netpay = newpay3 - Y4tax;

            printf("Year 4: tax = %f, net pay = %f\n", Y4tax, netpay);

            newpay4 = (newpay3 * 0.025) + newpay3;
            Y5tax = newpay4 * 0.25;
            netpay = newpay4 - Y5tax;

            printf("Year 5: tax = %f, net pay = %f\n", Y5tax, netpay);
            break;

        case 'B':
            printf("The increment is: %f\n", inc2);
            printf("Increment and tax over the five years\n");

            newpay = (salary * 0.015) + salary;

            Y1tax = newpay * 0.25;
            netpay = newpay - Y1tax;

            printf("Year 1: tax = %f, net pay = %f\n", Y1tax, netpay);

            newpay1 = (newpay * 0.015) + newpay;
            Y2tax = newpay1 * 0.25;
            netpay = newpay1 - Y2tax;

            printf("Year 2: tax = %f, net pay = %f\n", Y2tax, netpay);


            newpay2 = (newpay1 * 0.015) + newpay1;
            Y3tax = newpay2 * 0.25;
            netpay = newpay2 - Y3tax;

            printf("Year 3: tax = %f, net pay = %f\n", Y3tax, netpay);

            newpay3 = (newpay2 * 0.015) + newpay2;
            Y4tax = newpay3 * 0.25;
            netpay = newpay3 - Y4tax;

            printf("Year 4: tax = %f, net pay = %f\n", Y4tax, netpay);

            newpay4 = (newpay3 * 0.015) + newpay3;
            Y5tax = newpay4 * 0.25;
            netpay = newpay4 - Y5tax;

            printf("Year 5: tax = %f, net pay = %f\n", Y5tax, netpay);
            break;

        case 'C':
            printf("The increment is: %f\n", inc3);
            printf("Increment and tax over the five years\n");

            int i = 1;
            while (i <= 5)
            {
                printf("Year %d: tax = %f, net pay = %f\n", i, taxtotal, netpaytotal);
                i = i + 1;
            }

        default: printf("Invalid grade\n");
        }
        char inputChar;
        printf("Do you want to continue (y/n)?: ");
        inputChar = _getch();
        printf("%c\n", inputChar);
        while ((inputChar != 'Y' && inputChar != 'y') && (inputChar != 'N' && inputChar != 'n'))
        {
            printf("Please enter y or n: ");
            inputChar = _getch();
            printf("%c\n", inputChar);
        }

    } 


while (inputChar == 'y');
if (inputChar == 'n');
system("pause");

}

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,441 questions
{count} votes

Accepted answer
  1. Petrus 【KIM】 456 Reputation points
    2021-11-26T01:41:06.04+00:00
     // function declare
     float calculate_tax(float amount, float tax_rate);
     float calculate_netpay(float amount, float tax);
     float increment_salary(float amount, float increment_rate);
    
     // main
     int main()
     {
     // ......
         char inputChar = 'y';
         do
         {
     // ......  
             // char inputChar; /* Remove this line ...*/
             printf("Do you want to continue (y/n)?: ");
             inputChar = _getch();
             printf("%c\n", inputChar);
             while ((inputChar != 'Y' && inputChar != 'y') && (inputChar != 'N' && inputChar != 'n'))
             {
                 printf("Please enter y or n: ");
                 inputChar = _getch();
                 printf("%c\n", inputChar);
             }
         }
         while (inputChar == 'y');
    
         if (inputChar == 'n');
         system("pause");
    
         return 0;
     }
    
     // function body
     float calculate_tax(float amount, float tax_rate)
     {
        float salary;
        float tax = 0.25;
    
        return (salary * tax);
     }
     float calculate_netpay(float amount, float tax)
     {
         // Some code
         return 0.0f;
     }
     float increment_salary(float amount, float increment_rate)
     {
         // Some code
         return 0.0f;
     }
    

2 additional answers

Sort by: Most helpful
  1. Petrus 【KIM】 456 Reputation points
    2021-11-24T01:13:48.86+00:00

    You just remove a line (191)... see below

    // function declare
    float calculate_tax(float amount, float tax_rate);
    float calculate_netpay(float amount, float tax);
    float increment_salary(float amount, float increment_rate);
    
    // main
    int main()
    {
    // ......
        char inputChar = 'y';
        do
        {
    // ......  
            // char inputChar; /* Remove this line ...*/
            printf("Do you want to continue (y/n)?: ");
            inputChar = _getch();
            printf("%c\n", inputChar);
            while ((inputChar != 'Y' && inputChar != 'y') && (inputChar != 'N' && inputChar != 'n'))
            {
                printf("Please enter y or n: ");
                inputChar = _getch();
                printf("%c\n", inputChar);
            }
        }
        while (inputChar == 'y');
    
        if (inputChar == 'n');
        system("pause");
    
        return 0;
    }
    
    // function body
    float calculate_tax(float amount, float tax_rate)
    {
        // Some code
        return 0.0f;
    }
    float calculate_netpay(float amount, float tax)
    {
        // Some code
        return 0.0f;
    }
    float increment_salary(float amount, float increment_rate)
    {
        // Some code
        return 0.0f;
    }
    
    0 comments No comments

  2. Hubert Mostek 41 Reputation points
    2021-11-24T17:17:52.017+00:00

    Hi @Petrus 【KIM】 ,

    thank you very much for your reply. y/n works fine now!

    However I am still not sure what code I can put when you say 'some code'. Could you please explain?