[try Beta version]
Not logged in

 
Re: Function to calculate Tax

May 17, 2017 at 2:56am
Hi
I am writing a function where is calculates TAX based on AGE and Income.
The error I get is below and I have been researching to find an explanation but
none seem to fit my issue
Error: [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive]

I am sure how I use the function in main is also not correct

Any help or hint would be very much appreciated


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

void Cal_Print(int age[], int INcome[]){
	int tax;
	if (age<55&&INcome <= 10000){
		tax = 0;
	}
	else if (age<55 && INcome >10000 && INcome<=50000){
					tax = INcome*10/100;
				}
	else if (age<55 &&INcome >50000 && INcome <=100000){
						tax =INcome*20/100;
					}
	else if (age<55&&INcome >100000){
							tax = INcome*30/100;
						}
	
	else if (age>=55 && age<80&&INcome <= 20000){
		tax = 0;
	}
	else if (age>=55&&age<80&&INcome >20000 && INcome<=70000){
					tax = INcome*10/100;
				}
	else if (age>=55&&age<80&&INcome >70000 && INcome<=150000){
						tax =INcome*20/100;
				}
	else if (age>=55&&age<80&&INcome >150000){
							tax = INcome*30/100;
						}

else if (age >= 80){
	tax = 0;
  }
}

int main()
{

int fnpfNbr[6] = {12345, 12361, 34763, 11224, 54129, 10717};
int size=6;
int Age[6] = {24, 19, 47, 50, 35, 26};
char lastname_initial[6] = {'F', 'B', 'H', 'H', 'R', 'B'};
int Income[] = {40000, 20000, 100000, 35000, 75000, 28000};
int Tax[6];

Cal_Print(Age[], Income[]);
for (int i =0; i < size; i++){
cout <<lastname_initial[i]<<"\t"<<fnpfNbr[i]<<"\t"<<Age[i]<<"\t"<<Income[i]<<"\t"<<Tax[i]<<"\n";
}

return 0;
}
May 17, 2017 at 3:14am
1
2
3
4
5
void Cal_Print(int age[], int INcome[]){
	int tax;
	if (age<55&&INcome <= 10000){
		tax = 0;
	}


Please note that int INcome[] is an array. You have to access array by index to access the desired integer element.

Without index, you cannot compare it with anything.
May 17, 2017 at 5:15am
Noted with thanks
May 20, 2017 at 12:29am
Hi Mantorr22,
I have Worked on the code and now it did compile BUT the tax calculated is not as per programmed.

I have tried other ways via research...unfortunately I am getting nowhere

Any help would very much appreciated

My compiled code below:
#include <iostream>
using namespace std;

//Tax calculation function
void Cal_Tax(int A, int I){//A for Age, I for Income (Parameters)
int tax;
if (A<55&&I <= 10000){
tax = 0;
}
else if (A<55 && I >10000 && I<=50000){
tax = I*10/100;
}
else if (A<55 &&I >50000 && I <=100000){
tax =I*20/100;
}
else if (A<55&&I >100000){
tax = I*30/100;
}

else if (A>=55 && A<80&&I <= 20000){
tax = 0;
}
else if (A>=55&&A<80&&I >20000 && I<=70000){
tax = I*10/100;
}
else if (A>=55&&A<80&&I >70000 && I<=150000){
tax =I*20/100;
}
else if (A>=55&&A<80&&I >150000){
tax = I*30/100;
}

else if (A >= 80){
tax = 0;
}
}

int main()
{

int FNPFNbr[6] = {12345, 12361, 34763, 11224, 54129, 10717};
int SIZE=6;
int AGE[6] = {24, 19, 47, 50, 35, 26};
char LASTNAME_initial[6] = {'F', 'B', 'H', 'H', 'R', 'B'};
int INCOME[6] = {40000, 20000, 100000, 35000, 75000, 28000};
double TAX[6];

Cal_Tax(AGE[0], INCOME[0]);
cout <<"\nLastname(initial)"<<"\t"<<"FNPF#"<<"\t"<<"AGE"<<"\t"<<"Income"<<"\t"<<"Tax\n";
cout <<"----------------"<<"\t"<<"------"<<"\t"<<"---"<<"\t"<<"------"<<"\t"<<"-----\n";
for (int i =0; i < SIZE; i++){
cout <<LASTNAME_initial[i]<<"\t\t\t"<<FNPFNbr[i]<<"\t"<<AGE[i]<<"\t"<<INCOME[i]<<"\t"<<TAX<<"\n";
}

return 0;
}
May 20, 2017 at 2:11am
What did you expect, what did you get?

\t"<<"Tax\n";

once again, you have array without []

you also do not seem to be filling tax[I] in with any values that I see....
Last edited on May 20, 2017 at 2:15am
May 20, 2017 at 3:00am
Hi jonnin,

Sorry, typo error! Yes, i did code tax[i]..

My code was the function to base on the INCOME array + the AGE array to auto compute the TAX array

For example:
Income 40,000, tax as coded should be 4000 (10% of income). However, it is giving me 2.65941e-307
Or 20,000, tax should be 2000 but it is giving me 1.50783e-307

My function coding is not correct and my problem is I am all confused correcting it.
May 20, 2017 at 7:14am
If you need some hints, you can have a look at the following example based on your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iomanip>
#include <iostream>

//Tax calculation function
double cal_Tax(int age, int income);

int main()
{
    int age[] = {24, 19, 47, 50, 35, 26};
    int income[] = {40000, 20000, 100000, 35000, 75000, 28000};
    
    int size = sizeof(age) / sizeof(int);
    double* tax = new double[size];
    for(int i=0; i<size; i++) {
        tax[i] = cal_Tax(age[i], income[i]);
    }

    std::cout << "\nLastname(initial)" << "\tFNPF#" << "\tAGE" << "\tIncome" << "\tTax";
    std::cout << "\n-----------------" << "\t-----" << "\t---" << "\t------" << "\t---\n";

    int fnpfnbr[] = {12345, 12361, 34763, 11224, 54129, 10717};
    char lastname_initial[] = {'F', 'B', 'H', 'H', 'R', 'B'};
    for (int i=0; i<size; i++) {
        std::cout << lastname_initial[i] << "\t\t\t" << fnpfnbr[i] 
                  << '\t' << age[i] << '\t' << income[i] 
                  << '\t' << tax[i] << '\n';
    }

    delete[] tax;
    return 0;
}

double cal_Tax(int age, int income) //A for Age, I for Income (Parameters)
{
    double incdoub = double(income);
    if(age < 55) {
        if(income <=  10000) {return 0;}
        if(income <=  50000) {return incdoub * 10/100;}
        if(income <= 100000) {return incdoub * 20/100;}
        return incdoub * 30/100;
    }

    else if (55 < age && age < 80) {
        if(income <=  20000) {return 0;}
        if(income <=  70000) {return incdoub * 10/100;}
        if(income <= 150000) {return incdoub * 20/100;}
        return incdoub * 30/100;
    }

    return 0;
}

May 20, 2017 at 2:29pm
Just like I said...

tax the array in main isn't populated. its full of random values.

cal-tax fills in the local variable tax, but it never bridges the gap.

you need something like this..

double cal_tax(...)
{
...
return tax;
}

...
for(...)
tax[i] = cal_tax(..stuff);



as a side note, I don't think any of the values should be int, but double instead. That is your design choice, but you may have problems with it if you happen to calculate x/y where it becomes zero unintentionally and you may get odd roundoff errors.
Last edited on May 20, 2017 at 2:31pm
May 20, 2017 at 10:44pm
Thank you very much Enoizat and Jonnin. After those hint and example, it now workout for me.

Have a great day!
Topic archived. No new replies allowed.