Functions, Errors

Ok so I am trying to write (just for practice) a little program that figures out the total net income of an individual after taxes. I am getting funky errors all over the place, I am a newbie, and I have been working on this code for a while. I have read a bunch of articles and still cant seem to figure out the function stuff please help.

Thanks :)
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

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

double hrsWorked;
double hrWage;
double totalTaxes;
double netIncome;
double taxes;
double income;

double func_totalTaxes(hrsWorked, hrWage, taxes)
//error: initializer expression list treated as compound expression
{
//error: expected ',' or ';' before '{' token

	(hrsWorked * hrWage) =  netIncome;
	return netIncome;

	(netIncome * taxes) = totalTaxes;
	return totalTaxes;
}


double  func_totalIncome(netIncome, totalTaxes)
//error: initializer expression list treated as compound expression
{
//error: expected ',' or ';' before '{' token
double income;

	 netIncome - totalTaxes = income;
	 
	 return income;
}


int main (int argc, char * const argv[]) {

	
	cout << "Please enter your hours worked: " << endl;
	 cin >> hrsWorked;
	cout << "Please enter your hourly wage: " << endl;
	 cin >> hrWage;
	cout << "Please enter your taxes in decimal form (ie: %5 = .05): " << endl;
	 cin >> taxes;
	
	 func_totalTaxes(hrsWorked, hrWage, taxes); 
//error: 'func_totalTaxes' cannot be used as a function

	 func_totalIncome(netIncome, totalTaxes);
//error: 'func_totalIncome' cannot be used as a function

	 cout << "Your total net income is: " << income << endl;
	    return 0;
}




1.
double func_totalTaxes(hrsWorked, hrWage, taxes)
is wrong, you should read a bit about function basics:
http://cplusplus.com/doc/tutorial/functions/
first few paragraphs should clarify everything

2.
(hrsWorked * hrWage) = netIncome;
a common l-value (left value) issue (you will discover these errors after you correct previous ones)
in C++ arithmetic expression can not be an l-value, e.g.
a+b=c is wrong, c = a+b is correct
btw, these braces () aren't really required - C++ is (lets say) compatible with common arithemtic laws

3.
avoid declaring global variables
http://cplusplus.com/forum/beginner/6958/

4.
1
2
3
#include <math.h>
#include <stdio.h>
#include <stdlib.h> 

stdio and stdlib aren't standard anymore

5.
I suggest to read this tutorial (user-friendly)
http://cplusplus.com/doc/tutorial/
Last edited on
Great stuff! Thank you so much!

Alex M.
not so fast, you still have a few inconveniences related to your functions.
Look at this part of code:
cout << "Your total net income is: " << income << endl;
The result will be 0, because you are trying to display that income variable declared in the global scope.




Except that variable totalTaxes is also equal to 0 - look at the function body of func_totalTaxes - you are returning only netIncome. Compiler is passing by the rest. Just delete that return netIncome; - should work fine.

Anyhow your code is still hard to read, why not do it like this? (without global variables and other useless ones)
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
#include <iostream>

using namespace std;

double myFunction(double hoursWorked, double  wagePerHour, double tax)
{
	return (1-tax)*(hoursWorked*wagePerHour);
        //without all extra variables:
        //double netIncome = hrsWorked * hrWage;
        //double totalTaxes = netIncome * taxes;
        //return netIncome - totalTaxes;
        //isn't that the same?
}

int main ()
{
	double hrsWorked;
	double hrWage;
	double taxes;

	cout << "Please enter your hours worked: " << endl;
	cin >> hrsWorked;
	cout << "Please enter your hourly wage: " << endl;
	cin >> hrWage;
	cout << "Please enter your taxes in decimal form (ie: %5 = .05): " << endl;
	cin >> taxes;

	cout << "Your total net income is: " << myFunction(hrsWorked,hrWage,taxes);
	
	return 0;
}
Last edited on
Ok new stuff:

It is working through with no errors, however once I put my vars through the functions it does not return any numbers.
See below:

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
#include <iostream>
using namespace std;


double funcTotalTaxes(double hrsWorked, double hrWage, double taxes)
{
        double netIncome = (hrsWorked * hrWage);
	double totalTaxes = (netIncome * taxes);
	return totalTaxes;
}


double funcTotalIncome(double netIncome, double totalTaxes)
{
        double income = (netIncome - totalTaxes);
	return income;
}

int main (int argc, char * const argv[]) {
	
	double hrsWorked;
        double hrWage;
	double taxes;
	double netIncome;
	double totalTaxes;
	double income;
	
	cout << "Please enter your hours worked: ";
	cin >> hrsWorked;
        cout << "Please enter your hourly wage: ";
	cin >> hrWage;
        cout << "Please enter your taxes in decimal form (ie: %5 = .05): ";
	cin >> taxes;

	funcTotalTaxes(hrsWorked, hrWage, taxes); 
        funcTotalIncome(netIncome, totalTaxes);
	
	cout << "Hrs: " << hrsWorked << endl;
        cout << "Wage: " <<  hrWage << endl;
        cout << "Taxes: " << taxes << endl;
        cout << "netIncome: " << netIncome << endl;
        cout << "totalTaxes: " << totalTaxes << endl;
        cout << "Income: " << income << endl;
	cout << "Your total net income is: " << income << endl;
	return 0;
}


Console output:
Please enter your hours worked: 10
Please enter your hourly wage: 20
Please enter your taxes in decimal form (ie: %5 = .05): .03
Hrs: 10
Wage: 20
Taxes: 0.03
netIncome: 0
totalTaxes: 0
Income: 8.69289e-311
Your total net income is: 8.69289e-311


Thanks again :-]
yes, exactly, these variables:
1
2
3
double netIncome;
double totalTaxes;
double income;

aren't initialized and you are trying to use them, that's why you get warnings/weird results.
These are local variables, known for the compiler only in the main() scope. It may be confusing for you, because you are using the same variable names within your functions scopes.
Last edited on
The flaw in your program is that your functions do not modify your variables

As in:
Your net income is a different variable in your main function and your tax function.
That may seem strange, but that is how functions work.

Instead, there are 2 options:
one, use global variables:
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
#include <iostream>
using namespace std;

double netIncome;
double income;
double totalTaxes;

double funcTotalTaxes(double hrsWorked, double hrWage, double taxes)
{
    income = (hrsWorked * hrWage);
	totalTaxes = (income * taxes);
	return totalTaxes;
}


double funcTotalIncome(double netIncome, double totalTaxes)
{
    netIncome = (income - totalTaxes);
	return income;
}

int main (int argc, char * const argv[]) {
	
	double hrsWorked;
	double hrWage;
	double taxes;
	
	cout << "Please enter your hours worked: ";
	cin >> hrsWorked;
        cout << "Please enter your hourly wage: ";
	cin >> hrWage;
        cout << "Please enter your taxes in decimal form (ie: %5 = .05): ";
	cin >> taxes;

	funcTotalTaxes(hrsWorked, hrWage, taxes); 
    funcTotalIncome(netIncome, totalTaxes);
	
	cout << "Hrs: " << hrsWorked << endl;
        cout << "Wage: " <<  hrWage << endl;
        cout << "Taxes: " << taxes << endl;
        cout << "income: " << income << endl;
        cout << "totalTaxes: " << totalTaxes << endl;
        cout << "netIncome: " << netIncome << endl;
	cout << "Your total net income is: " << netIncome << endl;
	return 0;
}

or two, use pointers if you know what they are
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
#include <iostream>
using namespace std;

double funcTotalTaxes(double hrsWorked, double hrWage, double taxes, double *income, double *totalTaxes)
{
    *income = (hrsWorked * hrWage);
	*totalTaxes = (*income * taxes);
	return *totalTaxes;
}


double funcTotalIncome(double *netIncome, double totalTaxes, double income)
{
    *netIncome = (income - totalTaxes);
	return income;
}

int main (int argc, char * const argv[]) {
	
	double hrsWorked;
	double hrWage;
	double taxes;
	double netIncome;
	double income;
	double totalTaxes;
	cout << "Please enter your hours worked: ";
	cin >> hrsWorked;
        cout << "Please enter your hourly wage: ";
	cin >> hrWage;
        cout << "Please enter your taxes in decimal form (ie: %5 = .05): ";
	cin >> taxes;

	funcTotalTaxes(hrsWorked, hrWage, taxes, &income, &totalTaxes); 
    funcTotalIncome(&netIncome, totalTaxes, income);
	
	cout << "Hrs: " << hrsWorked << endl;
        cout << "Wage: " <<  hrWage << endl;
        cout << "Taxes: " << taxes << endl;
        cout << "income: " << income << endl;
        cout << "totalTaxes: " << totalTaxes << endl;
        cout << "netIncome: " << netIncome << endl;
	cout << "Your total net income is: " << netIncome << endl;
	return 0;
}

these are 2 of the many solutions to your problem (sorry for the formatting errors)
also, your net income and income were reversed.
I tried the same thing and came up with two errors. Here is my version:

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
#include <iostream>
using namespace std;

double hrWage;
double hrsWorked;
double totalTaxes;
double netIncome;
double taxes;
double income;
double totalincome;

double Tincome(hrWage, hrsWorked)
{
  double totalincome = hrWage * hrsWorked;
  return totalincome;
}

double Ttaxes(totalincome)
{
  double netIncome;
  totalTaxes = netIncome*0.19 + netIncome*0.06 + netIncome*0.01 + netIncome*0.06;
  netIncome = totalincome - totalTaxes;
  return netIncome;
}


int main()
{
  cout<<"Please enter your hourly wage: ";
  cin >> hrWage;
  cout<<"\nPlease enter your hours worked: ";
  cin >> hrsWorked;
  cout<<"Your total income is...\n\n";
  totalincome = Tincome(hrWage, hrsWorked);
  cin.get();
}


I get:
"File31.cpp": E2293 ) expected at line 12
"File31.cpp": E2141 Declaration syntax error at line 36
Right, I forgot to mention that that is an incomplete version before the rest is added in.
Anyone?
You aren't defining the types of your paramters to Ttaxes and Tincome.
Ooooooooh hahaha thanks a bunch draco, I can't believe I forgot that. Talk about a breakthrough moment.
Topic archived. No new replies allowed.