I'm a programming student whose textbook is a worthless piece of crap (errors in the text). Google and forums such as this one are all that are getting me by, since the text has me entering code that doesn't compile. The course is online, and there are no lectures- just assigned readings.
Anyhow, I have an assignment where I have to build a payroll calculator and calculate various variables. Here are my issues:
1)The first grossPay outputs (lines 42 and 44) end up beginning with a zero. It didn't do that as I built that section of code.
2)The program doesn't recognize that lines 42 and 44 are supplying the number that becomes the variable "grossPay".
3)The taxation section produces accurate figures, but I don't know how to make the program realize that the state tax and federal tax sections are producing results that I want to fill the stateTax and federalTax variables, respectively.
You are calculating with non-initialized values here.
In C++ you can only calculate when a variable is defined. Move these lines to after you get input for the variables in the equation.
Like the first one regularPay = hoursWorked * payRate; should be after cin >> payRate;
To set up federalTax and stateTax you need to include a initalizing statement in your if else's where you are outputting there values to the user. Like so:
1 2 3 4 5
if ( grossPay < 500 ) {
cout << "State Tax is 0 \n" << endl;
stateTax = 0.0;
}
else { //more statements
EDIT:
You can shrink some of your repetitious couts for your State and Federal tax by assigning the value to them then printing it in one statement.
1 2 3 4 5 6 7 8
if (grossPay < 500) // A better if... else if statement. Note the else if, and the last statement is an else.
stateTax = 0.0;
elseif (grossPay >= 1000)
stateTax = grossPay * 0.07;
else
stateTax = grossPay * 0.05;
cout << "The State Tax is " << stateTax << endl;
EDIT2: ((I really need to check out this code fully before typing my replies and hitting submit))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
if ( hoursWorked > 40 )
grossPay = 1.5 * payRate * hoursWorked;
else // You don't need an else if here. else will cover the numbers less then 40.
grossPay = hoursWorked * payRate;
cout << "Gross pay is: " << grossPay << endl;
// No need for cin, because grossPay is carried until the end of main()
cout << "\n\n""****************************************\n"" State & Federal Taxes \n""****************************************\n\n";
// Yes, this is a legal C++ statement. In compilation these will all be merged into one string for cout to print.
// Cin is not needed since grossPay is defined.
// ... code from above
Put some [code][/code] tags around that next time.
Well, the problem appears to be that you don't completely understand that programming in C++ is sequential. For example, when you perform the assignments at the beginning, they are done right then; they are not "delayed" until you print the variable or something.
Since those variables contain garbage (you haven't initialized them), they results will most likely be garbage.
I'm afraid I don't understand the initialization of the tax values...
You mean this?
1 2 3 4 5 6 7 8 9
if (grossPay < 500)
stateTax = 0.0; // the assignment is done if and only if gross pay is 499 or less
elseif (grossPay >= 1000)
stateTax = grossPay * 0.07; // Another line of assignment. Fills the value of stateTax with the calculation to the right.
// For clarification the equal sign is done right to left, so all calculations on the right are done before any assignment to any value on the left.
else
stateTax = grossPay * 0.05;
cout << "The State Tax is " << stateTax << endl; // Cout only prints the value. No assigning should happen here.
Along with FireDraco's Comment, values can only be initialized by variables that have been already been initialized, and no assignment happens everywhere in the code. Those statements where you have them happen right then and there, and anything that is in variables before they are assigned (they have values, called GARBAGE. Its whatever the memory now allocated for those values had in them before being used for these values, interpreted as to their current type.) and then used in your calculations.
Best way to explain this is run this:
1 2 3 4 5 6
int two = 2; // defined and initalized
int three, one; // defined but NOT initalized
int four = two+two;
int six = two + three + one;
cout << "The value of four is " << four << "\nand the value of six is " << six << endl;
And note the value of six which was made with uninitialized values
I love the code you've posted, but it gives my compiler errors (I'm running Dev C on Windows). I have similar issues with the code in my textbook... I don't know what is up with Dev.
DevC++ is no longer maintained and has a broken, outdated compiler. You should install wxDevC++ for a more modern compiler (with the same look and feel of DevC++) or install a better IDE like codeblocks, or Visual Studio Express.
I've got everything running the way I need now. I will look into alternate compilers. I now move on to other areas of the assignment, which I'm sure will necessitate my return here. You are a gentleman and a scholar, thank you.
Okay, check the folder with the saved .cpp file and see if the executable is there.
Then open cmd.exe (use the Run Dialog to open) and navigate to the same folder using cd <path_to_folder>. Then run the .exe through cmd and see if it comes up. If that runs that way then there's something wrong with wxdev.