Today class was really rushed, last class of the semester until the final. He gave us a very, very basic function program and 5 homework assignments. I have all but one done and I cannot figure out how to use a function twice to calculate two different variables.
He wants us to have a function compute the city tax, then it uses the function a second time to compute the county tax.
My program compiles, and runs fine, however it only outputs the city tax calculation. It seems it never attempts to calculate the county tax.
I wont see him until next Thursday (our final day) and this is going to be on the final, so I would like to attempt to get it right. Any help is greatly appreciated.
One thing: All of the directives I used is what we are limited to, no more, no less. He requires a set format and will dock points of you use anything but those directives and void main().
I am using Visual studio 2010. Normally I don't go to forums for help on my homework and attempt to figure it out, but with the little information I was given I am pretty much stuck right now.
Appreciate anybodies help on this. Thank you in advance. I think I know what I want to do, but really not sure how to implement it.
A function can only return one value. You've got a few options. First, you could create a struct which contains two doubles. You then return this struct, and get the values of the struct.
Secondly, you could create a boolean to determine whether you've already calculated the city taxes, and if you have, calculate the county tax instead. This would work with the main() you've given us.
EDIT: In more detail, the moment you use the return statement, the function instantly exits and returns that value. Any statements after that will never be executed.
If it's allowed by the assignment, just place it outside a function.
Also, don't use a double. Use a bool variable.
Make sure to set the value of the boolean after you've calculated the variable.
You could also make the function take less space by getting rid of the unnecessary variable r_county_tax/r_city_tax. Simply do this return gross_pay * country_tax_rate;
Thanks for the help! I did place it below the constants, however I believe this is considered a global variable? If so we are NOT allowed to use these.
Sadly it did work that way, but I'm pretty sure that would get me an automatic 0. He is rather particular with his formats and doesn't really show us how to do these types of things.
Sort of annoying, such an easy thing to take care of if I just created another function, but the assignment says to use the function twice >.<
I see. That complicates things.
The next thing I thought of is using a staticbool inside a function, but this could also be considered bad by your teacher.
A static variable retains it's value between function calls, basically. So defining a staticbool would allow the value to be "defined once".
But as stated previously, this could be another one of those things which would give you a 0.
I am thinking he typed this up without even thinking about it, because he gave us it in the last 5 minutes of class today.
Without using a global variable or static bool, there is no other way to return two different values in that one statement he wants us to use then?
Edit: Actually now that I look at it, he doesnt even have us outputting the county tax, just the city tax. I guess he just wants it to go back and calculate the tax? That would be this then?
If you don't use the r_county_tax variable anywhere, what's the point of defining it in the first place?
As far as I know, you'll either need to return a struct, or you need to use a variable which can keep it's state between function calls. I don't personally believe there is any other way to return two different values.
EDIT: I didn't even think about taking in reference variables. That's another way of doing that.
He requires a set format and will dock points of you use anything but those directives and void main().
Ugh.
Have you covered classes/structs in class? I'm assuming that get_taxes and get_city are supposed to be the same function, is this correct?
Are you allowed to change the signature of the function in question (change its parameters/return type)?