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

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    Angry 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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > gst_payable = gross_gst-rebate;
    > printf( "Your GST payable is %.2f\n" /*what goes here?*/ );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by salem View Post
    > gst_payable = gross_gst-rebate;
    > printf( "your gst payable is %.2f\n" /*what goes here?*/ );
    oops!

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    LOL -_-

    AHHHHHHHHHHHHHHHHHHHHHHHHHHHH

    Thanks Salem!

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    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. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by justiliang View Post
    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?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    Quote Originally Posted by AndrewHunter View Post
    You should always set warnings to max. What compiler are you using?
    Visual Studios Express 2010
    The one with C++ and C.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Salem View Post
    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.
    Last edited by AndrewHunter; 09-24-2011 at 03:28 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    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....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is the output of this code
    By C_Enthusiast in forum C Programming
    Replies: 2
    Last Post: 01-07-2011, 06:07 AM
  2. what is the output of this code?
    By karthik537 in forum C Programming
    Replies: 15
    Last Post: 09-16-2009, 11:20 AM
  3. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  4. What will be the output of the following code?
    By developersubham in forum C++ Programming
    Replies: 13
    Last Post: 01-02-2007, 07:36 AM
  5. same code different output
    By kashifk in forum C Programming
    Replies: 4
    Last Post: 03-18-2003, 02:51 PM