|
 |
|
 |
03-06-2006, 07:14 PM
|
#1 (permalink)
|
|
Completely friggin' lost.
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
|
Vanilla C problem...
I've got most of this worked out correctly, I've just got one last little bug I can't seem to figure out.
I was to write a program out for a C programming class that calculated some property taxes and then displayed the totals at the end, when the user was finished. I'm 99% sure I've got all my math right, but no matter what for my "residential taxes total" I end up with zero. It's puzzling me.
Also, when entering a residential property that is NOT a primary residence, then entering another residential, it will not ask if it is the primary residence again that time. But if tried a third time, the question appears again. It seems if answering 'n' to the primary residence question it skips over that bit the next time it rolls around. (Perhaps it's picking up a "\n" or another extra character somewhere??)
Here's a copy of my code...
If anyone can spot the problem, I'd be ever so grateful.
Code:
char getType(char type);
double getValue(double value);
double taxRes(char *primaryRes ,double value, int *countRes, double *totalRes);
double taxCom(double value, int *countCom, double *totalCom);
double taxAgr(double value, int *countAgr, double *totalAgr);
char printSummary(double totalTax, int countRes, int totalRes, int countCom, double totalCom, int countAgr, double totalAgr);
main() {
int countRes = 0, countCom = 0, countAgr = 0;
double value, totalTax = 0, totalRes = 0, totalCom = 0, totalAgr = 0;
char type, primaryRes = 'n';
printf("Welcome to the tax calculator\n");
while ((type = getType(type)) != 'q') {
value = getValue(value);
if (type == 'r')
totalTax = totalTax + taxRes(&primaryRes, value, &countRes, &totalRes);
else if (type == 'c')
totalTax = totalTax + taxCom(value, &countCom, &totalCom);
else
totalTax = totalTax + taxAgr(value, &countAgr, &totalAgr);
}
printSummary(totalTax, countRes, totalRes, countCom, totalCom, countAgr, totalAgr);
}
char getType(char type) {
char proptype = 'x';
while (proptype != 'c' || proptype != 'a' || proptype != 'r' || proptype != 'q'){
printf ("Enter Property Type ('r' for residential, 'c' for commerical, 'a' for agricultural, 'q' to quit):");
scanf (" %c", &proptype);
if (proptype == 'c' || proptype == 'a' || proptype == 'r' || proptype == 'q')
return proptype;
else
printf("Invalid value, please retry.\n");
}
}
double getValue(double value) {
double propvalue = -1;
while (propvalue < 0) {
printf ("Enter Property Value: $");
scanf ("%lf", &propvalue);
if (propvalue > 0) {
return propvalue;
}
else if (propvalue <= 0);
printf("Invalid amount, please re-enter.\n");
}
}
double taxRes(char *primaryRes, double value, int *countRes, double *totalRes) {
double restax;
char primeresreply = 'x';
if (*primaryRes != 'y') {
printf("Is this your primary residence?(y or n):");
scanf(" %c", &primeresreply);
if (primeresreply = 'y') {
*primaryRes = 'y';
restax = value * 0.021;
*countRes += 1;
*totalRes = restax + *totalRes;
return restax;
}
else if (primeresreply = 'n') {
*primaryRes = 'n';
restax = value * 0.029;
*countRes += 1;
*totalRes = restax + *totalRes;
return restax;
}
}
else if (primeresreply = 'n') {
*primaryRes = 'n';
restax = value * 0.029;
*countRes += 1;
*totalRes = restax + *totalRes;
return restax;
}
else
printf("Invalid reply, please retry.\n");
}
double taxCom(double value, int *countCom, double *totalCom) {
double comtax;
char retailreply;
while (retailreply != 'y' || retailreply != 'n') {
printf("Is this commerical building being used for retail sales? (y or n):");
scanf(" %c", &retailreply);
if (retailreply = 'y') {
comtax = value * 0.037;
*totalCom = comtax + *totalCom;
*countCom += 1;
return comtax;
}
else if (retailreply = 'n') {
comtax = value * 0.032;
*totalCom = comtax + *totalCom;
*countCom += 1;
return comtax;
}
else
printf("Invalid reply, please retry.\n");
}
}
double taxAgr(double value, int *countAgr, double *totalAgr) {
double agrtax;
agrtax = value * 0.032;
*countAgr += 1;
*totalAgr = agrtax + *totalAgr;
return agrtax;
}
char printSummary(double totalTax, int countRes, int totalRes, int countCom, double totalCom, int countAgr, double totalAgr) {
printf("**************************************************\n");
printf("Tax Summary(All totals are in $):\n");
printf("**************************************************\n");
printf("Tax on %10d residential properties is: %8.2lf\n", countRes, totalRes);
printf("Tax on %10d commercial properties is: %9.2lf\n", countCom, totalCom);
printf("Tax on %10d agricultural properties is: %6.2lf\n", countAgr, totalAgr);
printf("**************************************************\n");
printf("Total tax due is: %30.2lf\n",totalTax);
}
Last edited by blackmars0; 03-07-2006 at 07:39 AM.
|
|
|
03-06-2006, 09:12 PM
|
#2 (permalink)
|
|
Registered User
Join Date: Feb 2006
Posts: 6
|
My first question is in getValue(double), why are you passing in the type?
In getValue propValue is uninitialized before it is used, so I think this is where your first bug is.
Might want to change the first few lines of double getValue(double value) to:
double propValue = -1;
while (propValue < 0){....
|
|
|
03-07-2006, 07:38 AM
|
#3 (permalink)
|
|
Completely friggin' lost.
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
|
Thanks for pointing out that propvalue bug, but both of my previous bugs still stand. I'll edit the above and post the updated code with propvalue set.
|
|
|
03-07-2006, 10:21 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Feb 2006
Posts: 6
|
In taxRes(...) it looks like you meant to put a while loop, but all you have is a set conditional statements, which looks like it might give you a zero. I would also recommend initializing any variable you use before assining a value to it. For instance, in taxCom(...) retailreply is used before it is initialized, and there are several other variables treated in the same manner.
Also, I dont know if you know this but you can simplify (for code reading) something like:
*totalRes = restax + *totalRes; to *totalRes += restax;
|
|
|
03-07-2006, 10:47 AM
|
#5 (permalink)
|
|
Completely friggin' lost.
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
|
I've figured out what was wrong with the program. Just stupid syntax stuff I got lazy on. Thanks anyways, though.
(For the record, I forgot to put down a second "=" sign on some of the if statements, and I made the totalRes an integer value in the last module, instead of a double value, which lead to the zero sum error.
|
|
|
03-07-2006, 01:00 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Feb 2006
Posts: 6
|
DOH! can't believe I missed that.
|
|
|
03-07-2006, 01:35 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Apr 2003
Posts: 34
|
Quote:
|
Originally Posted by blackmars0
I've figured out what was wrong with the program. Just stupid syntax stuff I got lazy on. Thanks anyways, though.
(For the record, I forgot to put down a second "=" sign on some of the if statements, and I made the totalRes an integer value in the last module, instead of a double value, which lead to the zero sum error.
|
A good way of avoiding that is to put the constant value on the left, rather than the right.
Reason being that:
won't generate an error, but
will, as you're trying to assign a value to 0, which is constant.
And don't worry, EVERYBODY gets that one once in a while.
Also, FWIW, I'd take a look at the parameters you're passing. You've got a lot of functions like getType() that take an input variable, but don't do anything with it, or even modify the value pointed to. In cases like that, there's no reason to pass the variable at all.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:02 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|