Getting negative results but my logic seems sound?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 558 Views - Last Post: 09 November 2018 - 09:18 AM Rate Topic: -----

#1 nightraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 17-November 17

Getting negative results but my logic seems sound?

Posted 07 November 2018 - 08:13 AM

So for an assignment, I have the following brief:

You will write a program for a small business (company). The program computes and prints the net
pay for different employees at the end of each month. It also computes and prints a summary of the
payroll for that month.


It is menu driven and the first menu option should Compute and Print the Pay for an Employee. This is done using a number of functions, as seen in my code here:

/********************************************************************
 *
 * I declare that the following program was written by me and that I
 * have NOT copied any part of this code from any other source.
 *
 * Name: George Strawbridge
 *
 * Email: [email protected]
 *
 * Date: 5/11/2018
 *
 * *********************************************************************/

/* This is a template with the functions you must implement for this assignment.
 *
 * Note that you are free to implement any additional function you consider appropriate.
 */
//variables relating to the tax of an individual employee

#include <stdio.h>
#include <stdlib.h>

float annualSalary, taxableIncome, taxableIncome, annualIncomeTax, annualNatInsurance, annualNatInsurance1, annualNatInsurance2, monthlyNatInsurance, monthlyNetPay, monthlyPreTax, monthlyIncomeTax, tempvar;
int age, personalAllowance = 11850;

//Returns the personal allowance of a person with the given annual salary
float compute_personal_allowance(float annualSalary) {
  if (annualSalary <= 100000) {
    personalAllowance = 11850;
  } else(annualSalary > 100000); {
    personalAllowance = 11850 - 1 * ((annualSalary - 100000) / 2);
  }

  return personalAllowance;
}

//Returns the taxable income of a person with the given annual salary and personal allowance
float compute_taxable_income(float annualSalary, float personalAllowance) {
  if (annualSalary < 11850); {
    taxableIncome = 0;
  }

  if (annualSalary <= 100000); {
    taxableIncome = annualSalary - personalAllowance;
  }
  if (annualSalary > 100000); {
    taxableIncome = annualSalary - (11850 - 1 * ((annualSalary - 100000) / 2));
  }
  return taxableIncome;
}

//Returns the annual income tax of a person with the given taxable income
float compute_annual_income_tax(float taxableIncome) {

  if (annualSalary <= 34500); {
    annualIncomeTax = ((taxableIncome * 20) / 100);
  }
  if (34501 <= annualSalary && 150000 <= annualSalary); {
    annualIncomeTax = ((taxableIncome * 40) / 100);
  }
  if (annualSalary > 150000); {
    annualIncomeTax = ((taxableIncome * 20)/100) + (40/100) * (taxableIncome - 34500);
  }

  return annualIncomeTax;
}

//Returns the annual national insurance for a person with the given annual salary and age.
float compute_annual_national_insurance(float annualSalary, int age) {

  if (age > 65) {
    annualNatInsurance = 0;
  } else(age < 65); {
    if (annualSalary < 8424) {
      annualNatInsurance = 0;
   } else if (8424 >= annualSalary && 46350 <= annualSalary); {
      annualNatInsurance = ((annualSalary * 12) / 100);
    }
    if (annualSalary > 46350) {
      if (annualSalary < 46350) {
        annualNatInsurance1 = ((annualSalary * 12) / 100);
     } else(annualSalary > 46350); {
        annualNatInsurance2 = ((annualSalary * 2) / 100);
      }
      annualNatInsurance = annualNatInsurance1 + annualNatInsurance2;
    }
    return annualNatInsurance;
  }
}

//Computes the monthly net pay of a person with the given annual salary, annual income tax, and annual national insurance
float compute_monthly_net_pay(float annualSalary, float annualIncomeTax, float annualNatInsurance) {

  monthlyPreTax = annualSalary / 12;
  monthlyIncomeTax = annualIncomeTax / 12;
  monthlyNatInsurance = annualNatInsurance / 12;

  monthlyNetPay = (monthlyPreTax) - (monthlyIncomeTax) - (monthlyNatInsurance);

  return monthlyNetPay;
}

/*Enters a person with a given annual salary and age into the system.

This#include <stdio.h>
  #include <stdlib.h> function uses the functions above to compute and update the cumulative statistics, namely:

A. The total amount of money paid out by the company to all employees.
B. The average net monthly salary.
C. The maximum income tax paid by any employee.
D. The minimum income tax paid by any employee.

hint: store the values into global variables, so that the functions below can return them.
*/
void add_person(int age, float annualSalary){
    printf("Age: %d\n", age);
    printf("Salary: %f\n", annualSalary);
}

//Returns the total money paid by the company to the employees entered
//into the system through add_person()
float get_total_money_paid() {

}

//Returns the average net salary of all the employees entered into the
//system through add_person()
float get_average_net_salary() {

}

//Returns the maximum income tax paid by any of the employees entered into the
//system through add_person()
float get_maximum_income_tax() {

}

//Returns the minumum income tax paid by any of the employees entered into the
//system through add_person()
float get_minimum_income_tax() {

}

int main() {

    //Declare our variables
    //Control variable for choosing menu option
    int menuChoice = 0;

  printf("Hello. Welcome to the program.\n");
  printf("Press any key to continue...\n");
  getchar();
  system("clear");

  while (menuChoice != 3) {

    //prints the menu
    printf("\nMain Menu\n");
    printf("-----------\n");
    printf("1. Compute and Print the Pay for an Employee\n"); //Menu for user to select what they want to do
    printf("2. Show Sums and Statistics\n");
    printf("3. Quit the program\n");
    printf("-----------\n\n");
    printf("Enter choice (1, 2, or 3)\n");
    scanf("%d", & menuChoice); //Scans and stores users input

    if (menuChoice == 1) //if statement for what to do depending on what option is selected by the user
    {
      printf("Please enter the age of the employee in years:\n");
      scanf("%d", &age);
      printf("Please enter the annual salary of the employee:\n");
      scanf("%f", &annualSalary);

      add_person(age, annualSalary);
      compute_personal_allowance(annualSalary);
      compute_taxable_income(annualSalary, personalAllowance);
      compute_annual_income_tax(taxableIncome);
      compute_annual_national_insurance(annualSalary, age);
      compute_monthly_net_pay(annualSalary, annualIncomeTax, annualNatInsurance);



      printf("Employee Age: %d\n", age);
      printf("Annual Salary: %f\n", annualSalary);
      printf("Taxable Income: %f\n", taxableIncome);
      printf("Monthly Income Tax: %f\n", monthlyIncomeTax);
      printf("Monthly National Insurance: %f\n", monthlyNatInsurance);
      printf("Net Monthly Pay: %f\n", monthlyNetPay);

    }
    return 0;
  }
}



So for example, I enter Age: 20 and Salary: 8000. This is what I get:

Age: 20
Salary: 8000.000000
Employee Age: 20
Annual Salary: 8000.000000
Taxable Income: -49850.000000
Monthly Income Tax: -830.833313
Monthly National Insurance: 80.000000
Net Monthly Pay: 1417.500000


I'm unsure as to why I'm getting negatives for both Taxable Income and Monthly Income Tax. I can't spot the mistake in my logic. Everything else seems okay though so that's at least something.

Any pointers guys? Would greatly appreciated.

PS. For this assignment, this is a requirement:

Compute the following monthly sums and statistics:
A.
The total amount of money paid out by the company to all employees.
B.
The average net monthly salary.
C.
The maximum income tax paid by any employee.
D.
The minimum income tax paid by any employee.


I am not allowed to use arrays, structs, or pointers, so what's the best way to go about storing data for multiple people? I'm guessing a for loop? Would appreciate a kick in the right direction. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting negative results but my logic seems sound?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 08:26 AM

Curious - why do you have two of the same variable?

023   float annualSalary, taxableIncome, taxableIncome, 

Was This Post Helpful? 0
  • +
  • -

#3 nightraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 17-November 17

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 08:31 AM

Complete oversign on my part - thanks for pointing it out. I've changed it now, but obviously that hasn't fixed the problem. There's a test harness for the project I just used and it's not looking good, seems a lot of my logic is flawed:

[[email protected] testHarness]$ gcc -lm -std=c99 -o tests unity.c test.c cw1.c
[[email protected] testHarness]$ ./tests
test.c:25:test_compute_personal_allowance:FAIL: Expected 11850 Was 17158
test.c:31:test_taxable_income:FAIL: Expected 77533 Was 72224.5
test.c:38:test_annual_income_tax:FAIL: Expected 24113.2 Was 15506.6
test.c:44:test_annual_national_insurance:FAIL: Expected 0 Was 1787.66
test.c:88:test_monthly_net_pay:PASS
Age: 88
Salary: 89383.000000
test.c:59:test_cumulative:FAIL: Expected 7448.58 Was 0. Error on Total Money Paid
 
-----------------------
6 Tests 5 Failures 0 Ignored
FAIL

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 08:36 AM

Do not put semicolons after the IF conditions, else the program assumes you are done and the body doesn't correctly engage.

039   if (annualSalary < 11850); {


should be

039  if (annualSalary < 11850) {


Also think about your logic. if 'annualSalary' is 8k.. then that satisfies the condition for line 39.. AND line 43. Is that what you want? Maybe an 'ELSE IF' would be good to chain the conditions together.

039  if (annualSalary < 11850); {
040    taxableIncome = 0;
041  }
042 
043  if (annualSalary <= 100000); {
044    taxableIncome = annualSalary - personalAllowance;
045  }



Also you have a mess of functions that have zero return values when they need to return a float. You really should crank up the warnings on your compiler.
Was This Post Helpful? 1
  • +
  • -

#5 nightraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 17-November 17

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 09:05 AM

I will deal with the return values once I have finished tackling the current problem.

This is my current code for my compute_personal_allowance function:

float compute_personal_allowance(float annualSalary) {
  if (annualSalary <= 100000) {
    personalAllowance = 11850;
}
  else if(annualSalary > 100000); {
    personalAllowance = 11850 - 1 * ((annualSalary - 100000) / 2);
  }

  return personalAllowance;
}


Yet the test for that still fails. I don't understand why. I clearly state that if the annualSalary is <= 100000 then the personalAllowance is set to 11850. If the annualSalary is above 100000, then it is worked out like the following example:

An employee has an annual salary of £121,500
The annual salary of this employee is greater than £100,000, hence, their personal allowance is
reduced by £1 for each £2 they earn over 100,000:
Personal allowance = 11,850 – 1 *
(
(121,500 - 100,000)/2
)
= 1,100
The taxable income is 121,500 – 1,100 = 120,400
The taxable income has reached band 2, therefore they pay:
Monthly income tax =
(
20% * 34500 + 40% * (120,400-34500)
)
/12 = £3438.33


But the test gives me this:

test.c:25:test_compute_personal_allowance:FAIL: Expected 11850 Was 17158
test.c:31:test_taxable_income:FAIL: Expected 77533 Was 72224.5
test.c:38:test_annual_income_tax:FAIL: Expected 24113.2 Was 15506.6
test.c:44:test_annual_national_insurance:FAIL: Expected 0 Was 1787.66
test.c:88:test_monthly_net_pay:PASS
Age: 88
Salary: 89383.000000
test.c:59:test_cumulative:FAIL: Expected 7448.58 Was 0. Error on Total Money Paid

-----------------------
6 Tests 5 Failures 0 Ignored 



How in the world is the personal allowance being stored as 17158 when my logic clearly states that it should equal 11850. Unless I'm just too tired and missing something completely obvious. This is starting to drive me up the walls a little. I really do appreciate the fast responses though.

Furthermore, the fact that it is failing the compute_annual_nat_insurance test too. My code clearly states that if the age of the employee is over 65, they pay no national insurance.

float compute_annual_national_insurance(float annualSalary, int age) {

  if (age > 65) {
    annualNatInsurance = 0;
  } else(age < 65); {
    if (annualSalary < 8424) {
      annualNatInsurance = 0;
   } else if (8424 >= annualSalary && 46350 <= annualSalary); {
      annualNatInsurance = ((annualSalary * 12) / 100);
    }
    if (annualSalary > 46350) {
      if (annualSalary < 46350) {
        annualNatInsurance1 = ((annualSalary * 12) / 100);
     } else(annualSalary > 46350); {
        annualNatInsurance2 = ((annualSalary * 2) / 100);
      }
      annualNatInsurance = annualNatInsurance1 + annualNatInsurance2;
    }
    return annualNatInsurance;
  }
}


Yet it is spitting out 1787.66 as the natInsurance. The test employee is clearly stated to be 88 so I'm confused.

This post has been edited by nightraven: 07 November 2018 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 09:10 AM

Copy/paste your current code.
Was This Post Helpful? 0
  • +
  • -

#7 nightraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 17-November 17

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 09:22 AM

/********************************************************************
 *
 * I declare that the following program was written by me and that I
 * have NOT copied any part of this code from any other source.
 *
 * Name: George Strawbridge
 *
 * Email: [email protected]
 *
 * Date: 5/11/2018
 *
 * *********************************************************************/

/* This is a template with the functions you must implement for this assignment.
 *
 * Note that you are free to implement any additional function you consider appropriate.
 */
//variables relating to the tax of an individual employee

#include <stdio.h>
#include <stdlib.h>

float annualSalary, taxableIncome, taxableIncome, annualIncomeTax, annualNatInsurance, annualNatInsurance1, annualNatInsurance2, monthlyNatInsurance, monthlyNetPay, monthlyPreTax, monthlyIncomeTax, tempvar;
int age, personalAllowance = 11850;

//Returns the personal allowance of a person with the given annual salary
float compute_personal_allowance(float annualSalary) {
  if (annualSalary <= 100000) {
    personalAllowance = 11850;
}
  else if(annualSalary > 100000); {
    personalAllowance = 11850 - 1 * ((annualSalary - 100000) / 2);
  }

  return personalAllowance;
}

//Returns the taxable income of a person with the given annual salary and personal allowance
float compute_taxable_income(float annualSalary, float personalAllowance) {
  if (annualSalary < 11850); {
    taxableIncome = 0;
  }

  if (annualSalary <= 100000); {
    taxableIncome = annualSalary - personalAllowance;
  }
  if (annualSalary > 100000); {
    taxableIncome = annualSalary - (11850 - 1 * ((annualSalary - 100000) / 2));
  }
  return taxableIncome;
}

//Returns the annual income tax of a person with the given taxable income
float compute_annual_income_tax(float taxableIncome) {

  if (annualSalary <= 34500); {
    annualIncomeTax = ((taxableIncome * 20) / 100);
  }
  if (34501 <= annualSalary && 150000 <= annualSalary); {
    annualIncomeTax = ((taxableIncome * 40) / 100);
  }
  if (annualSalary > 150000); {
    annualIncomeTax = ((taxableIncome * 20)/100) + (40/100) * (taxableIncome - 34500);
  }

  return annualIncomeTax;
}

//Returns the annual national insurance for a person with the given annual salary and age.
float compute_annual_national_insurance(float annualSalary, int age) {

  if (age > 65) {
    annualNatInsurance = 0;
  } else(age < 65); {
    if (annualSalary < 8424) {
      annualNatInsurance = 0;
   } else if (8424 >= annualSalary && 46350 <= annualSalary); {
      annualNatInsurance = ((annualSalary * 12) / 100);
    }
    if (annualSalary > 46350) {
      if (annualSalary < 46350) {
        annualNatInsurance1 = ((annualSalary * 12) / 100);
     } else(annualSalary > 46350); {
        annualNatInsurance2 = ((annualSalary * 2) / 100);
      }
      annualNatInsurance = annualNatInsurance1 + annualNatInsurance2;
    }
    return annualNatInsurance;
  }
}

//Computes the monthly net pay of a person with the given annual salary, annual income tax, and annual national insurance
float compute_monthly_net_pay(float annualSalary, float annualIncomeTax, float annualNatInsurance) {

  monthlyPreTax = annualSalary / 12;
  monthlyIncomeTax = annualIncomeTax / 12;
  monthlyNatInsurance = annualNatInsurance / 12;

  monthlyNetPay = (monthlyPreTax) - (monthlyIncomeTax) - (monthlyNatInsurance);

  return monthlyNetPay;
}

/*Enters a person with a given annual salary and age into the system.

This#include <stdio.h>
  #include <stdlib.h> function uses the functions above to compute and update the cumulative statistics, namely:

A. The total amount of money paid out by the company to all employees.
B. The average net monthly salary.
C. The maximum income tax paid by any employee.
D. The minimum income tax paid by any employee.

hint: store the values into global variables, so that the functions below can return them.
*/
void add_person(int age, float annualSalary){
    printf("Age: %d\n", age);
    printf("Salary: %f\n", annualSalary);
}

//Returns the total money paid by the company to the employees entered
//into the system through add_person()
float get_total_money_paid() {

}

//Returns the average net salary of all the employees entered into the
//system through add_person()
float get_average_net_salary() {

}

//Returns the maximum income tax paid by any of the employees entered into the
//system through add_person()
float get_maximum_income_tax() {

}

//Returns the minumum income tax paid by any of the employees entered into the
//system through add_person()
float get_minimum_income_tax() {

}

int mymain() {

    //Declare our variables
    //Control variable for choosing menu option
    int menuChoice = 0;

  printf("Hello. Welcome to the program.\n");
  printf("Press any key to continue...\n");
  getchar();
  system("clear");

  while (menuChoice != 3) {

    //prints the menu
    printf("\nMain Menu\n");
    printf("-----------\n");
    printf("1. Compute and Print the Pay for an Employee\n"); //Menu for user to select what they want to do
    printf("2. Show Sums and Statistics\n");
    printf("3. Quit the program\n");
    printf("-----------\n\n");
    printf("Enter choice (1, 2, or 3)\n");
    scanf("%d", & menuChoice); //Scans and stores users input

    if (menuChoice == 1) //if statement for what to do depending on what option is selected by the user
    {
      printf("Please enter the age of the employee in years:\n");
      scanf("%d", &age);
      printf("Please enter the annual salary of the employee:\n");
      scanf("%f", &annualSalary);

      add_person(age, annualSalary);
      compute_personal_allowance(annualSalary);
      compute_taxable_income(annualSalary, personalAllowance);
      compute_annual_income_tax(taxableIncome);
      compute_annual_national_insurance(annualSalary, age);
      compute_monthly_net_pay(annualSalary, annualIncomeTax, annualNatInsurance);



      printf("Employee Age: %d\n", age);
      printf("Annual Salary: %f\n", annualSalary);
      printf("Taxable Income: %f\n", taxableIncome);
      printf("Monthly Income Tax: %f\n", monthlyIncomeTax);
      printf("Monthly National Insurance: %f\n", monthlyNatInsurance);
      printf("Net Monthly Pay: %f\n", monthlyNetPay);

    }
    return 0;
  }
}



To compile the program you will need to chage mymain() back to main(). It just needs to be changed to mymain() when using the testharness but that doesn't really matter I guess since you don't have the harness created for this project.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 09:26 AM

You still have a ton of IF statements with ; after them. Fix that.
Also have your empty functions return 0.
You also have the same two variables in there.

Seriously - fix these small issues.

What compiler are you using?
Was This Post Helpful? 1
  • +
  • -

#9 nightraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 17-November 17

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 09:28 AM

gcc on CentOS. I will fix all of those issues and get back to you.
Was This Post Helpful? 0
  • +
  • -

#10 nightraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 17-November 17

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 09:47 AM

I have fixed said issues:

/********************************************************************
 *
 * I declare that the following program was written by me and that I
 * have NOT copied any part of this code from any other source.
 *
 * Name: George Strawbridge
 *
 * Email: [email protected]
 *
 * Date: 5/11/2018
 *
 * *********************************************************************/

/* This is a template with the functions you must implement for this assignment.
 *
 * Note that you are free to implement any additional function you consider appropriate.
 */
//variables relating to the tax of an individual employee

#include <stdio.h>
#include <stdlib.h>

float annualSalary, taxableIncome, annualIncomeTax, annualNatInsurance, annualNatInsurance1, annualNatInsurance2, monthlyNatInsurance, monthlyNetPay, monthlyPreTax, monthlyIncomeTax, tempvar;
int age, personalAllowance = 11850;

//Returns the personal allowance of a person with the given annual salary
float compute_personal_allowance(float annualSalary) {
  if (annualSalary <= 100000) {
    personalAllowance = 11850;
}
  else if(annualSalary > 100000) {
    personalAllowance = 11850 - 1 * ((annualSalary - 100000) / 2);
  }

  return personalAllowance;
}

//Returns the taxable income of a person with the given annual salary and personal allowance
float compute_taxable_income(float annualSalary, float personalAllowance) {
  if (annualSalary < 11850) {
    taxableIncome = 0;
  }

  if (annualSalary <= 100000) {
    taxableIncome = annualSalary - personalAllowance;
  }
  if (annualSalary > 100000) {
    taxableIncome = annualSalary - (11850 - 1 * ((annualSalary - 100000) / 2));
  }
  return taxableIncome;
}

//Returns the annual income tax of a person with the given taxable income
float compute_annual_income_tax(float taxableIncome) {

  if (annualSalary <= 34500) {
    annualIncomeTax = ((taxableIncome * 20) / 100);
  }
  if (34501 <= annualSalary && 150000 <= annualSalary) {
    annualIncomeTax = ((taxableIncome * 40) / 100);
  }
  if (annualSalary > 150000) {
    annualIncomeTax = ((taxableIncome * 20)/100) + (40/100) * (taxableIncome - 34500);
  }

  return annualIncomeTax;
}

//Returns the annual national insurance for a person with the given annual salary and age.
float compute_annual_national_insurance(float annualSalary, int age) {

  if (age > 65) {
    annualNatInsurance = 0;
  } else (age < 65) {
       if (annualSalary < 8424) {
          annualNatInsurance = 0;

   } else if (8424 >= annualSalary && 46350 <= annualSalary) {
      annualNatInsurance = ((annualSalary * 12) / 100);
    }
    if (annualSalary > 46350) {
      if (annualSalary < 46350) {
        annualNatInsurance1 = ((annualSalary * 12) / 100);
     } else(annualSalary > 46350) {
        annualNatInsurance2 = ((annualSalary * 2) / 100);
      }
      annualNatInsurance = annualNatInsurance1 + annualNatInsurance2;
    }
    return annualNatInsurance;
  }
}

//Computes the monthly net pay of a person with the given annual salary, annual income tax, and annual national insurance
float compute_monthly_net_pay(float annualSalary, float annualIncomeTax, float annualNatInsurance) {

  monthlyPreTax = annualSalary / 12;
  monthlyIncomeTax = annualIncomeTax / 12;
  monthlyNatInsurance = annualNatInsurance / 12;

  monthlyNetPay = (monthlyPreTax) - (monthlyIncomeTax) - (monthlyNatInsurance);

  return monthlyNetPay;
}

/*Enters a person with a given annual salary and age into the system.

This#include <stdio.h>
  #include <stdlib.h> function uses the functions above to compute and update the cumulative statistics, namely:

A. The total amount of money paid out by the company to all employees.
B. The average net monthly salary.
C. The maximum income tax paid by any employee.
D. The minimum income tax paid by any employee.

hint: store the values into global variables, so that the functions below can return them.
*/
void add_person(int age, float annualSalary){
    printf("Age: %d\n", age);
    printf("Salary: %f\n", annualSalary);
}

//Returns the total money paid by the company to the employees entered
//into the system through add_person()
float get_total_money_paid() {

   return 0;

}

//Returns the average net salary of all the employees entered into the
//system through add_person()
float get_average_net_salary() {

   return 0;

}

//Returns the maximum income tax paid by any of the employees entered into the
//system through add_person()
float get_maximum_income_tax() {

   return 0;

}

//Returns the minumum income tax paid by any of the employees entered into the
//system through add_person()
float get_minimum_income_tax() {

   return 0;

}

int main() {

    //Declare our variables
    //Control variable for choosing menu option
    int menuChoice = 0;

  printf("Hello. Welcome to the program.\n");
  printf("Press any key to continue...\n");
  getchar();
  system("clear");

  while (menuChoice != 3) {

    //prints the menu
    printf("\nMain Menu\n");
    printf("-----------\n");
    printf("1. Compute and Print the Pay for an Employee\n"); //Menu for user to select what they want to do
    printf("2. Show Sums and Statistics\n");
    printf("3. Quit the program\n");
    printf("-----------\n\n");
    printf("Enter choice (1, 2, or 3)\n");
    scanf("%d", & menuChoice); //Scans and stores users input

    if (menuChoice == 1) //if statement for what to do depending on what option is selected by the user
    {
      printf("Please enter the age of the employee in years:\n");
      scanf("%d", &age);
      printf("Please enter the annual salary of the employee:\n");
      scanf("%f", &annualSalary);

      add_person(age, annualSalary);
      compute_personal_allowance(annualSalary);
      compute_taxable_income(annualSalary, personalAllowance);
      compute_annual_income_tax(taxableIncome);
      compute_annual_national_insurance(annualSalary, age);
      compute_monthly_net_pay(annualSalary, annualIncomeTax, annualNatInsurance);



      printf("Employee Age: %d\n", age);
      printf("Annual Salary: %f\n", annualSalary);
      printf("Taxable Income: %f\n", taxableIncome);
      printf("Monthly Income Tax: %f\n", monthlyIncomeTax);
      printf("Monthly National Insurance: %f\n", monthlyNatInsurance);
      printf("Net Monthly Pay: %f\n", monthlyNetPay);

    }
    return 0;
  }
}



But now I receive a compiler error:

$ gcc -lm -std=c99 cw1.c -o cw.c
cw1.c: In function ‘compute_annual_national_insurance’:
cw1.c:75:21: error: expected ‘;’ before ‘{’ token



So there seems to be a problem with my braces placement...
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 09:52 AM

Think about what IF and ELSE do. IF provides a condition. Either it is true and the IF body is executed or the ELSE happens. Does it make sense to have a condition in your ELSE unless it is an ELSE IF? No, it doesn't.

072  if (age > 65) {
073    annualNatInsurance = 0;
074  } else (age < 65) {



Fix it there and anywhere else you have an ELSE sans an IF but with a condition.
Was This Post Helpful? 0
  • +
  • -

#12 nightraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 17-November 17

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 10:12 AM

Thanks for how helpful you've been and for explaining things well. I've ended up with this:

/********************************************************************
 *
 * I declare that the following program was written by me and that I
 * have NOT copied any part of this code from any other source.
 *
 * Name: George Strawbridge
 *
 * Email: [email protected]
 *
 * Date: 5/11/2018
 *
 * *********************************************************************/

/* This is a template with the functions you must implement for this assignment.
 *
 * Note that you are free to implement any additional function you consider appropriate.
 */
//variables relating to the tax of an individual employee

#include <stdio.h>
#include <stdlib.h>

float annualSalary, taxableIncome, annualIncomeTax, annualNatInsurance, annualNatInsurance1, annualNatInsurance2, monthlyNatInsurance, monthlyNetPay, monthlyPreTax, monthlyIncomeTax, tempvar;
int age, personalAllowance = 11850;

//Returns the personal allowance of a person with the given annual salary
float compute_personal_allowance(float annualSalary) {
  if (annualSalary <= 100000) {
    personalAllowance = 11850;
}
  else if(annualSalary > 100000) {
    personalAllowance = 11850 - 1 * ((annualSalary - 100000) / 2);
  }

  return personalAllowance;
}

//Returns the taxable income of a person with the given annual salary and personal allowance
float compute_taxable_income(float annualSalary, float personalAllowance) {
  if (annualSalary < 11850) {
    taxableIncome = 0;
  }

  else if (annualSalary <= 100000) {
    taxableIncome = annualSalary - personalAllowance;
  }
  else if (annualSalary > 100000) {
    taxableIncome = annualSalary - (11850 - 1 * ((annualSalary - 100000) / 2));
  }
  return taxableIncome;
}

//Returns the annual income tax of a person with the given taxable income
float compute_annual_income_tax(float taxableIncome) {

  if (annualSalary <= 34500) {
    annualIncomeTax = ((taxableIncome * 20) / 100);
  }
  else if (34501 <= annualSalary && 150000 <= annualSalary) {
    annualIncomeTax = ((taxableIncome * 40) / 100);
  }
  else if (annualSalary > 150000) {
    annualIncomeTax = ((taxableIncome * 20)/100) + (40/100) * (taxableIncome - 34500);
  }

  return annualIncomeTax;
}

//Returns the annual national insurance for a person with the given annual salary and age.
float compute_annual_national_insurance(float annualSalary, int age) {

  if (age > 65) {
    annualNatInsurance = 0;
} else if (age < 65) {
       if (annualSalary < 8424) {
          annualNatInsurance = 0;

   } else if (8424 >= annualSalary && 46350 <= annualSalary) {
      annualNatInsurance = ((annualSalary * 12) / 100);
    }
    if (annualSalary > 46350) {
      if (annualSalary < 46350) {
        annualNatInsurance1 = ((annualSalary * 12) / 100);
     } else if (annualSalary > 46350) {
        annualNatInsurance2 = ((annualSalary * 2) / 100);
      }
      annualNatInsurance = annualNatInsurance1 + annualNatInsurance2;
    }
    return annualNatInsurance;
  }
}

//Computes the monthly net pay of a person with the given annual salary, annual income tax, and annual national insurance
float compute_monthly_net_pay(float annualSalary, float annualIncomeTax, float annualNatInsurance) {

  monthlyPreTax = annualSalary / 12;
  monthlyIncomeTax = annualIncomeTax / 12;
  monthlyNatInsurance = annualNatInsurance / 12;

  monthlyNetPay = (monthlyPreTax) - (monthlyIncomeTax) - (monthlyNatInsurance);

  return monthlyNetPay;
}

/*Enters a person with a given annual salary and age into the system.

This#include <stdio.h>
  #include <stdlib.h> function uses the functions above to compute and update the cumulative statistics, namely:

A. The total amount of money paid out by the company to all employees.
B. The average net monthly salary.
C. The maximum income tax paid by any employee.
D. The minimum income tax paid by any employee.

hint: store the values into global variables, so that the functions below can return them.
*/
void add_person(int age, float annualSalary){
    printf("Age: %d\n", age);
    printf("Salary: %f\n", annualSalary);
}

//Returns the total money paid by the company to the employees entered
//into the system through add_person()
float get_total_money_paid() {

   return 0;

}

//Returns the average net salary of all the employees entered into the
//system through add_person()
float get_average_net_salary() {

   return 0;

}

//Returns the maximum income tax paid by any of the employees entered into the
//system through add_person()
float get_maximum_income_tax() {

   return 0;

}

//Returns the minumum income tax paid by any of the employees entered into the
//system through add_person()
float get_minimum_income_tax() {

   return 0;

}

int main() {

    //Declare our variables
    //Control variable for choosing menu option
    int menuChoice = 0;

  printf("Hello. Welcome to the program.\n");
  printf("Press any key to continue...\n");
  getchar();
  system("clear");

  while (menuChoice != 3) {

    //prints the menu
    printf("\nMain Menu\n");
    printf("-----------\n");
    printf("1. Compute and Print the Pay for an Employee\n"); //Menu for user to select what they want to do
    printf("2. Show Sums and Statistics\n");
    printf("3. Quit the program\n");
    printf("-----------\n\n");
    printf("Enter choice (1, 2, or 3)\n");
    scanf("%d", & menuChoice); //Scans and stores users input

    if (menuChoice == 1) //if statement for what to do depending on what option is selected by the user
    {
      printf("Please enter the age of the employee in years:\n");
      scanf("%d", &age);
      printf("Please enter the annual salary of the employee:\n");
      scanf("%f", &annualSalary);

      add_person(age, annualSalary);
      compute_personal_allowance(annualSalary);
      compute_taxable_income(annualSalary, personalAllowance);
      compute_annual_income_tax(taxableIncome);
      compute_annual_national_insurance(annualSalary, age);
      compute_monthly_net_pay(annualSalary, annualIncomeTax, annualNatInsurance);



      printf("Employee Age: %d\n", age);
      printf("Annual Salary: %f\n", annualSalary);
      printf("Taxable Income: %f\n", taxableIncome);
      printf("Monthly Income Tax: %f\n", monthlyIncomeTax);
      printf("Monthly National Insurance: %f\n", monthlyNatInsurance);
      printf("Net Monthly Pay: %f\n", monthlyNetPay);

    }
    return 0;
  }
}



Looking good so far?

The thing is, when I run and test myself I get the results that I would expect. They look correct:

Please enter the age of the employee in years:
20
Please enter the annual salary of the employee:
10000
Age: 20
Salary: 10000.000000
Employee Age: 20
Annual Salary: 10000.000000
Taxable Income: 0.000000
Monthly Income Tax: 0.000000
Monthly National Insurance: 0.000000
Net Monthly Pay: 833.333313



but when using the test harness I am still having issues:

[[email protected] testHarness]$ ./tests
test.c:25:test_compute_personal_allowance:FAIL: Expected 0 Was -12046
test.c:31:test_taxable_income:FAIL: Expected 147793 Was 159840
test.c:38:test_annual_income_tax:FAIL: Expected 24113.2 Was 15506.6
test.c:44:test_annual_national_insurance:FAIL: Expected 5479.66 Was 1855.54
test.c:88:test_monthly_net_pay:PASS
Age: 88
Salary: 89383.000000
test.c:59:test_cumulative:FAIL: Expected 7448.58 Was 0. Error on Total Money Paid



Damn.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 10:20 AM

It's like the Plinko game. The disk bounces on the pegs and lands in a bucket. That's your IF and ELSE. If you have an IF and ELSEIF but no ELSE there's no bucket to catch the disk and it falls off the board.

Example - I need something to print.

		int a = 6;

		if (a < 1)
		{ 
			printf("1");
		}
		else if (a < 5)
		{
			printf("5");
		}


IF 6 < 1 // nope!
ELSE IF 6 < 5 // NOPE!
// nothing prints.


		if (a < 1)
		{ 
			printf("1");
		}
		else if (a < 5)
		{
			printf("5");
		}
		else
		{
			printf("bigger");
		}


IF 6 < 1 // nope!
ELSE IF 6 < 5 // NOPE!
ELSE prints 'bigger'!

ELSe is all about what happens when the IF condition is not met. Sometimes you need one.. sometimes you don't.. but if you are going to evaluate many similar IF statements and only need one to happen it is best to have an ELSE.
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1547
  • View blog
  • Posts: 4,911
  • Joined: 20-March 10

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 12:09 PM

BIDMAS,

ever heard of it?

Brackets Indices Division Multiplication Addition Subtraction

The rules governing which arithmetic process happens first

You have where the salary is over £100,000

the equation

personalAllowance = 11850 - 1 * ((annualSalary - 100000) / 2);

this states that -1* (a large figure) happens first which probably means you end up with a large negative personal allowance. so that can't be right can it ? your personal allowance cant fall below 0

so your logical thinking needs a warm bath and relax and try again.
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: Getting negative results but my logic seems sound?

Posted 07 November 2018 - 12:21 PM

Heh.. damn UK. It's PEMDAS! Parens, exponents, multiplication, division, add, subtract. :D :^:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2