question

HubertMostek-7800 avatar image
0 Votes"
HubertMostek-7800 asked HubertMostek-7800 commented

tax calucaltion functions c++

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++
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You create the new functions the same way you created main. You already have prototypes for these functions in your text. Simply copy these prototypes to just before the start of main. Then add each function after the end of main. Since you have already coded the statements that do the work in main, you can copy these to each of the functions as appropriate. You will need to add a return statement to each function to pass the computed value back to main. Finally, replace the statements in main that you copied to the functions with a call to the appropriate function.

While you are adjusting the code, consider the following:

  1. Since you want to accept multiple inputs, why do you insist everyone have the same ID number (line 33)?

  2. What purpose is served by having a for loop that iterates only once (line 51)?

  3. scanf_s will accept input until you hit Enter. When the system echoes the Enter, it will go to a new line. Did you intend to have a double space between each type of input (line 31, 47, etc)?

  4. You cannot reach the code that processes Grade if the salary was detected out of range before printing payroll data. Testing the range again is superfluous (line 82).

  5. The grade processing code is common to all grades except for the value of the increment. If your switch statement was limited to simply assigning the correct increment value, the remaining code would not need to be repeated.


0 Votes 0 ·

@HubertMostek-7800 I got an error: initialization of 'i' is skipped by 'default' label when I ran the program. You could add { } in the case such as:

 case 'A' :
 {
 //some code
 }

If you have any questions after modifying the code, please feel free to ask.






0 Votes 0 ·
PetrusKIM-2099 avatar image
0 Votes"
PetrusKIM-2099 answered HubertMostek-7800 commented
  // 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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Two of your functions still have an incorrect return statement.

You have chosen to omit the most important parts of your code so we have no idea if you are using the functions correctly or if the last two functions perform the desired tasks.

There is a logical inconsistency between your code at line 18 and your code at line 25. Ditto for lines 18 and 27.

In function calculate_tax, why is tax_rate an unused parameter.

0 Votes 0 ·

Thank you very much for your help :)

0 Votes 0 ·
PetrusKIM-2099 avatar image
0 Votes"
PetrusKIM-2099 answered PetrusKIM-2099 edited

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;
 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HubertMostek-7800 avatar image
0 Votes"
HubertMostek-7800 answered HubertMostek-7800 commented

Hi @PetrusKIM-2099 ,

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?

· 10
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The some code is the code that performs the actual calculations which was already present in main but needs to be moved to the functions.

0 Votes 0 ·

Yes.. Right!

0 Votes 0 ·

Thanks for your answers guys!

Now with the below code I get this errors. Trying to figure this out but with no success .


   float calculateTax(float salary, float tax);
     {
         float salary;
         float tax = 0.25
        
         calculateTax = salary * 0.25;
         return 0.0f;
     }

Errors:
Error C2449 found '{' at file scope (missing function header?);
Error C2059 syntax error: '}'

0 Votes 0 ·
Show more comments