Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-06-2006, 07:14 PM   #1 (permalink)
blackmars0
Completely friggin' lost.
 
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
blackmars0 is on a distinguished road
Send a message via MSN to blackmars0
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.
blackmars0 is offline   Reply With Quote
Old 03-06-2006, 09:12 PM   #2 (permalink)
demonicsoftware
Registered User
 
Join Date: Feb 2006
Posts: 6
demonicsoftware is on a distinguished road
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){....
demonicsoftware is offline   Reply With Quote
Old 03-07-2006, 07:38 AM   #3 (permalink)
blackmars0
Completely friggin' lost.
 
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
blackmars0 is on a distinguished road
Send a message via MSN to blackmars0
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.
blackmars0 is offline   Reply With Quote
Old 03-07-2006, 10:21 AM   #4 (permalink)
demonicsoftware
Registered User
 
Join Date: Feb 2006
Posts: 6
demonicsoftware is on a distinguished road
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;
demonicsoftware is offline   Reply With Quote
Old 03-07-2006, 10:47 AM   #5 (permalink)
blackmars0
Completely friggin' lost.
 
Join Date: Mar 2006
Location: Whitby, Ontario
Posts: 15
blackmars0 is on a distinguished road
Send a message via MSN to 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.
blackmars0 is offline   Reply With Quote
Old 03-07-2006, 01:00 PM   #6 (permalink)
demonicsoftware
Registered User
 
Join Date: Feb 2006
Posts: 6
demonicsoftware is on a distinguished road
DOH! can't believe I missed that.
demonicsoftware is offline   Reply With Quote
Old 03-07-2006, 01:35 PM   #7 (permalink)
kyoryu
Registered User
 
Join Date: Apr 2003
Posts: 34
kyoryu is on a distinguished road
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.

Code:
if( 0 == val )
...
Reason being that:
Code:
if( val = 0 )
...
won't generate an error, but

Code:
if( 0 = val )
...
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.
kyoryu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kaat a talking bot in c nvictor Platform/API C++ 10 05-19-2005 02:16 PM
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 10:43 PM
C problem saurabh1905 Standard C, C++ 2 06-11-2004 02:00 AM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 05:02 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting