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.
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
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)
#include <iostream>
usingnamespace 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
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.