Thread: I think my calculation is wrong

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    I think my calculation is wrong

    I'm continuing to work on this water billproject of mine and its going well but I think my calculation is wrong. Here's the code:

    Code:
        #include <stdio.h>
    
    	int main()
    
    {
    	int		customernumber;
    	float	waterlevel;
    	float	previouslevel;
    	float	totalwater;
    	float	totalbill;
    	float	amountowed;
    
    
    	printf("Enter customer number\n" );
    
    	scanf( "%d" ,  &customernumber );
    
    	printf("Enter current water level\n" );
    
    	scanf("%f" , &waterlevel );
    
    	printf("Enter previous water level\n" );
    
    	scanf("%f", &previouslevel );
    
    	totalwater = waterlevel - previouslevel;
    
    	totalbill = totalwater/1000  * .55f;
    
    printf("The total bill is %f\n", &amountowed );
    No matter what numbers I input I get 0.0000 for the totalbill, once again I'm at a loss as to why.
    Any feedback is more then appreciated,
    thank you so much.
    -Extro
    Edit: Sorry I forgot to post the final line of code.
    Last edited by Extropian; 06-08-2005 at 10:40 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What are the values you are using for input? Where is the line of code that outputs the bill?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    68
    The amount entered should be any number in the thousands and I get a monetary result.
    Thanks,
    Extro

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I meant what specific values were you using but that doesn't matter now because I know what's going on with your code... see below.

    Quote Originally Posted by Extropian
    I'm continuing to work on this water billproject of mine and its going well but I think my calculation is wrong. Here's the code:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int   customernumber;
        float waterlevel;
        float previouslevel;
        float totalwater;
        float totalbill;
        float amountowed;
    
        printf("Enter customer number\n" );
        scanf( "%d" ,  &customernumber );
    
        printf("Enter current water level\n" );
        scanf("%f" , &waterlevel );
    
        printf("Enter previous water level\n" );
        scanf("%f", &previouslevel );
    
        totalwater = waterlevel - previouslevel;
    
        totalbill = totalwater/1000  * .55f;
    
        printf("The total bill is %f\n", &amountowed );
    No matter what numbers I input I get 0.0000 for the totalbill, once again I'm at a loss as to why.
    Any feedback is more then appreciated,
    thank you so much.
    -Extro
    Edit: Sorry I forgot to post the final line of code.
    You aren't giving amountowed a value anywhere beyond what it starts out with. You calculate totalbill but try to print out something else entirely. Did you mean to print out totalbill instead? Also, don't use the & in front of values you are printing out.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    68
    Ok thank you, one more quick more questions if thats ok (I know all about the howework rule and I agree with it, so i hope I'm not asking too much).
    My question is,
    How do I get his to read out in dollars?
    I.e. instead of 1.1000 it would read $1.10
    Thanks again.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main()
    {
       double amount = 1.1;
       printf("amount = $%.2f\n", amount);
       return 0;
    }
    
    /* my output
    amount = $1.10
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("The total bill is %f\n", &amountowed );
    1. Remove the & - this is printf, not scanf
    2. Get a compiler (eg. gcc) which will tell you when you make dumb mistakes with printf and scanf.
    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.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    68
    One last question:

    I want to say if amount is greater than 5000 but less than or equal to 75000 how would I do that?
    I think the code would look somewhat like this:
    Code:
     if (amountowed > 5000 <= 75000)
    {
    totalbill = totalbill * .2f;
    }
    this is obviously wrong, I'm just not sure what that condition should look like.
    Thanks a bunch
    -Extro

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    if ( amountowed > 5000 && amountowed <= 75000 )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    68
    Well I finished that program, thank for all the help everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  2. can someone check my code and tell me what I'm doing wrong
    By jlmac2001 in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2003, 08:14 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM