# Thread: Need help with an error in the output of my code!

1. ## Need help with an error in the output of my code!

Hello everyone! I recently started programming 2 weeks ago and for one of the practice assignments I keep on getting an output of 0.00 no matter what number I plug in. I have spent two hours on this and still have the same issue .

Only one aspect of this code works, when I plug in a negative number I get the output of "Error".

Here is the assignment (I don't think you need to read this to uncover the error):
In this part of the prelab exercise you will write a program to help a prospective home buyer determine the amount of GST payable on a new home. Your program must prompt the user for the purchase price of the home and then print the GST payable on the screen. If the purchase price is negative, your program must print an error message and do nothing else. You should assume that the purchase price is an integer (in other words, a house is unlikely to have a selling price of 1.2 million dollars and 42 cents!)
We'll assume that the GST payable on a new home is computed as follows (note, these aren't the rules that are actually used but they're close)
1. the gross amount of tax payable is computed as 5% of the purchase price of the home
2. a rebate on the tax computed in step 1 is then calculated according to the following scheme:
1. if the purchase price is less than or equal to \$350,000.00, then the rebate is 36% of the value computed in step 1) to a maximum of \$5,000.00
2. if the purchase price is strictly between \$350,000.00 and \$450,000.00, then the rebate is computed as: 5,000*(450,000.00-purchase price)/100,000.00
3. if the purchase price is \$450,000.00 or more, then there is no rebate
3. The GST payable is the amount computed in step (1) less the amount computed in step (2)
As for my code:
Code:
/*Author: Justin Liang
Date: Sept 23 2011
Purpose: Finds GST payable on a house
Prelab 2.2
*/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define GST 0.05

int main( void )
{
int houseprice;
double rebate;
double gross_gst;
double gst_payable;

printf( "What is the cost of the house\?\n" );
scanf( "%i", &houseprice );

gross_gst = houseprice*GST;

if( houseprice<0 )
{
printf( "Error\n" );
}
else
{
if( houseprice<=350000 )
{
rebate = 0.36*houseprice;
if( rebate>5000 )
{
rebate = 5000;
}
}
else
{
if( houseprice>350000 && houseprice<450000 )
{
rebate = 5000*(450000-houseprice)/100000;
}
else
{
rebate = 0;
}
}

gst_payable = gross_gst-rebate;
printf( "Your GST payable is %.2f\n" );
}

system( "PAUSE" );
return 0;
}

2. > gst_payable = gross_gst-rebate;
> printf( "Your GST payable is %.2f\n" /*what goes here?*/ );

3. Originally Posted by salem
> gst_payable = gross_gst-rebate;
> printf( "your gst payable is %.2f\n" /*what goes here?*/ );
oops!

4. LOL -_-

AHHHHHHHHHHHHHHHHHHHHHHHHHHHH

Thanks Salem!

5. This is why you always compile with warnings turned to max. Additionally, if your output is wrong the first thing to check is your output functions.

6. How do I set it to max? I probably will only use it for dire situations since we are not allowed these tools on tests; we have to write it out by hand.

7. Originally Posted by justiliang
How do I set it to max? I probably will only use it for dire situations since we are not allowed these tools on tests; we have to write it out by hand.
You should always set warnings to max. What compiler are you using?

8. Originally Posted by AndrewHunter
You should always set warnings to max. What compiler are you using?
Visual Studios Express 2010
The one with C++ and C.

9. Go to:
1. Project->Properties (That opens a dialog box)
2. Configuration Properties->C/C++
3. Ensure "Warning Level" is EnableAllWarnings(/Wall)
4. Ensure "Treat Warnings as Errors" as Yes(/Wx)

Note: For Visual Studio the last part is kindof a pain in the but. It will flag scanf, and many other functions as warnings and hence stop compilation. Although a nice interface, I don't necessarily reccomend Visual Studio for people still learning how to program as some of the "warnings" will not make sense. Take a look at Pelles C (Common Tater's sig) or gcc(windows port) if you are just starting to learn to program.

Note Note: If you want to stick with Visual Studio, then you can enable your projects to ignore specific warnings, e.g. the scanf one.

10. Do Pelles and/or VS even report problems with mis-matched printf/scanf formats?
I'm only aware of gcc being able to do this.

11. Originally Posted by Salem
Do Pelles and/or VS even report problems with mis-matched printf/scanf formats?
I'm only aware of gcc being able to do this.
I know gcc does. As for Pelles C I am clueless, just assumed based on Tater's constant proposing. As for Visual Studio, I guess I will see, my comment was more or less aimed as to why you should always compile with warnings at max.

EDIT: removed nonsense.

12. Originally Posted by Salem
Do Pelles and/or VS even report problems with mis-matched printf/scanf formats?
I'm only aware of gcc being able to do this.
Pelles does. Can't speak for Vividly Sinister, though....