trying to implement 2 functions into my code

I'm trying to implement two functions in my code RequiredData() needs nmarital status, gross salary, and perecentage contribution & TaxCalculator()needs the return of the tax value after performing the required computations to calculate the tax. what is wrong with my code?

#include <iostream>
#include <iomanip>

using namespace std;
void RequiredData();
void TaxCalculator(double tax);
int main()
{

const double MARRIED_DEDUCTION = 9000.00;
const double SINGLE_DEDUCTION = 3000.00;
const double PERSONAL_EXECPTION = 1800.00;
char maritalStatus;
int numberOfChildren;
double grossSalary;
double pContributedToPension;
double taxableAmount;
double sDeduction;
double tax;


int numberOfDeduction;}



void RequiredData()
{

const double MARRIED_DEDUCTION = 9000.00;
const double SINGLE_DEDUCTION = 3000.00;
const double PERSONAL_EXECPTION = 1800.00;
char maritalStatus;
int numberOfChildren;
double grossSalary;
double pContributedToPension;
double taxableAmount;
double sDeduction;
double tax;
double numberOfDeduction;

cout << fixed << showpoint;
cout << setprecision(2);


cout << "Enter marital status: m or M (married), s or S (single): ";
cin >> maritalStatus;
cout << endl;

if (maritalStatus == 'm' || maritalStatus == 'M')
{
cout << "Enter number of children under the age of 14: ";
cin >> numberOfChildren;
cout << endl;
}

if(maritalStatus == 'm' || maritalStatus == 'M')
{
numberOfDeduction = 2 + numberOfChildren;
sDeduction = MARRIED_DEDUCTION;
}
else
{
numberOfDeduction = 1;
sDeduction = SINGLE_DEDUCTION;

cout << "Enter gross salary for the year: ";
cin >> grossSalary;
cout << endl;

cout << "Enter percentage of salary contributed to pension (0-9 percent): ";
cin >> pContributedToPension;
cout << endl;


}



}
TaxCalculator()
{
double grossSalary;
double pContributedToPension;
double taxableAmount;
double sDeduction;
double tax;
double numberOfDeduction;
const double PERSONAL_EXECPTION = 1800.00;

taxableAmount = grossSalary - sDeduction -
PERSONAL_EXECPTION * numberOfDeduction -
grossSalary * pContributedToPension / 100;


if (taxableAmount < 0)
taxableAmount = 0;

if (0 <= taxableAmount && taxableAmount <= 15000)
tax = taxableAmount * .15;
else if (15001 <= taxableAmount && taxableAmount <= 40000)
tax = 2250 + (taxableAmount - 15000) * .25;
else
tax = 8460 + (taxableAmount - 40000) * .35;

cout << endl;
cout << "Your tax for the year is: "<<tax;


}
Hello dmo,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can us the preview button at the bottom to see how it looks.

Some of the first things that I see are: TaxCalculator()needs the return of the tax value after performing the required computations to calculate the tax., your function returns nothing. What you need is: double TaxCalculator();.

The first three line of main, the "const double"s would be better used as global variables defined before main. This way you will not have to redefine them again in your functions.

I also noticed that all three functions define some of the same variables. The problem is that "RequiredData()" will get the input from the user and all that information will be lost when the function goes out of scope and the other two functions will have no values for their variables. You should also initialize the variables when you define them to avoid problems and its good form.

These are what jumped out first. I will look into the logic of the program in a little while.

Hope that helps,

Andy

P.S. main is missing a "return 0;"
Last edited on
Hello dmo,

After working on your code for awhile I found there are some problems with your code. I think it will be easier to go through the comments in the code than write everything out here.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//  Empty Project.cpp

#include <iostream>
#include <iomanip>
#include <conio.h>  //  for _getch()
#include <chrono>
#include <thread>


void RequiredData(char &maritalStatus, int &numberOfChildren, double &grossSalary, double &pContributedToPension,
	double &taxableAmount, double &sDeduction, double &tax, double &numberOfDeduction);
void TaxCalculator(double tax);

// Moved these here. They work better as golbal variabls 
const double MARRIED_DEDUCTION = 9000.00;
const double SINGLE_DEDUCTION = 3000.00;
const double PERSONAL_EXECPTION = 1800.00;

int main()
{
	//  Used to make the program wait for a number of seconds.
	// std::this_thread::sleep_for(std::chrono::seconds(3));  // include "chrono" and "thread" header files
	// std::this_thread::sleep_for(std::chrono::milliseconds(1000));

	//  Initalize all variabls.
	char maritalStatus{ ' ' };
	int numberOfChildren{ 0 };
	double grossSalary{ 0.0 };
	double pContributedToPension{ 0.0 };  // <--- Careful pContributedToPension the beginning "p" could be preceived a meaning a pointer.
	double taxableAmount{ 0.0 };
	double sDeduction{ 0.0 };
	double tax{ 0.0 };

	double numberOfDeduction{ 0.0 };

	//  Added this line to test the program.
	//  The way I believe the function should be called
	RequiredData(maritalStatus, numberOfChildren, grossSalary, pContributedToPension, taxableAmount,
		sDeduction, tax, numberOfDeduction);

	// A similar type of call to "TaxCalculator" would be needed.


	// ****** Used to kep the console window open during testing.
	std::cout << "\n\n\n\nFin Press any key to continue -- > ";
	_getch();

	return 0;  // <--- Added
}  //  End main


//void RequiredData() // <--- Your original.
void RequiredData(char &maritalStatus, int &numberOfChildren, double &grossSalary, double &pContributedToPension,
	double &taxableAmount, double &sDeduction, double &tax, double &numberOfDeduction)
{
	// ***********************************************************
	// duplication defination of a global variable not needed here.

	//const double MARRIED_DEDUCTION = 9000.00;
	//const double SINGLE_DEDUCTION = 3000.00;
	//const double PERSONAL_EXECPTION = 1800.00;
	// ***********************************************************
	//  These variavles should only be defined once and not here. Consider passing them as a refrence
	//  to the function. Otherwise their values will be lost when the function goes out of scope.

	//  passed into function. Commented these variables out due to the changes I made.
	//char maritalStatus;
	//int numberOfChildren;
	//double grossSalary;
	//double pContributedToPension;
	//double taxableAmount;
	//double sDeduction;
	//double tax;
	//double numberOfDeduction{ 0.0 };

	std::cout << std::fixed << std::showpoint;
	std::cout << std::setprecision(2);


	std::cout << "Enter marital status: m or M (married), s or S (single): ";
	std::cin >> maritalStatus;
	std::cout << std::endl;

	// You might conider using maritalStatus = toupper(maritalStatus); and in the if statement
	// if(maritalStatus == 'M') or just if(toupper(maritalStatus) == 'M')

	if (maritalStatus == 'm' || maritalStatus == 'M')
	{
		std::cout << "Enter number of children under the age of 14: ";
		std::cin >> numberOfChildren;
		std::cout << std::endl;
	}

	if (maritalStatus == 'm' || maritalStatus == 'M')
	{
		numberOfDeduction = 2 + numberOfChildren;
		sDeduction = MARRIED_DEDUCTION;
	}
	else
	{
		numberOfDeduction = 1;
		sDeduction = SINGLE_DEDUCTION;

		std::cout << "Enter gross salary for the year: ";
		std::cin >> grossSalary;
		std::cout << std::endl;

		std::cout << "Enter percentage of salary contributed to pension (0-9 percent): ";
		std::cin >> pContributedToPension;
		std::cout << std::endl;
	}
}


void TaxCalculator(double tax)  // <--- Changed to match the proto type at the beginning of the program.
// tax should be passed as a referenc or rturned when the function ends.
// But rturnning tax is just a guess for now.
{
	//  These variavles should only be defined once and not here. Consider passing them as a refrence
	//  to the function.
	double grossSalary{ 0.0 };
	double pContributedToPension{ 0.0 };
	double taxableAmount{ 0.0 };
	double sDeduction{ 0.0 };
	double tax;  // <--- Redefination of what was passed to the function.
	double numberOfDeduction{ 0.0 };
	const double PERSONAL_EXECPTION = 1800.00; // should be defined as a global variable and not neded here.

	// consider order of precedence some () may be needed.
	taxableAmount = grossSalary - sDeduction -
		PERSONAL_EXECPTION * numberOfDeduction -
		grossSalary * pContributedToPension / 100;


	if (taxableAmount < 0)
		taxableAmount = 0;

	if (0 <= taxableAmount && taxableAmount <= 15000)
		tax = taxableAmount * .15;
	else if (15001 <= taxableAmount && taxableAmount <= 40000)
		tax = 2250 + (taxableAmount - 15000) * .25;
	else
		tax = 8460 + (taxableAmount - 40000) * .35;

	std::cout << std::endl;
	std::cout << "Your tax for the year is: " << tax;
}


I did get the program working. Do you have any test data that you use where you know the answer?

Hope that helps,

Andy
Topic archived. No new replies allowed.