Thread: Program keeps printing 0

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Program keeps printing 0

    Code:
    #include <stdio.h>
    
    
    int main ()  {
    
    int amount;
    
    scanf("Enter in an amount in dollars %d\n", &amount);
    
    amount = amount + amount *.05;
    printf("Your total amount is %d\n", amount);
    
    
    return 0;
    
    
    }
    This is pretty embarrassing, but i cant fix this logic error, any hints? Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you notice how your "prompt" isn't printed to the screen? And are you typing in exactly
    Code:
    Enter in an amount in dollars 55
    at that lack-of-prompt?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Which is to say, you need to print your prompt message to the user, and THEN have your scanf() line of code.

    This is NOT BASIC (which does it that way).

    Multiplying an integer by .05 will not do anything you like. There are two ways to deal with this:

    1) use a float or a double data type for the money, but not ALL values can be represented by these data types.

    2) Get the money in as a string, and then remove the decimal point if it has one. Then transfer it over to your money variable as an int, but it will be in pennies - not dollars.

    Which sounds weird, but it's actually perfect - integers are perfect for pennies because you never have fractions of a penny, in real life.
    Last edited by Adak; 04-25-2010 at 10:58 AM.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, for optimum results don't do floating point arithmetic and store results in an integer.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Thanks Adask, that helped a lot. @claudiu, wouldnt there be a precision error in the final answer if i do it this way?

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by purplehaze View Post
    Thanks Adask, that helped a lot. @claudiu, wouldnt there be a precision error in the final answer if i do it this way?
    If you want a floating point number then why not just make your variable a double and use the %lf flag instead of %d?

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Quote Originally Posted by claudiu View Post
    If you want a floating point number then why not just make your variable a double and use the %lf flag instead of %d?
    Ah, ok. You see, from the C book im reading i was under the impression that a variable declared as float x ; for example was a double and that the way to make a variable a float is to use the "f" keyword such as float x = 4.0f. Anyhow, i'll remember the %lf, thanks.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by purplehaze View Post
    Ah, ok. You see, from the C book im reading i was under the impression that a variable declared as float x ; for example was a double and that the way to make a variable a float is to use the "f" keyword such as float x = 4.0f. Anyhow, i'll remember the %lf, thanks.
    Floats are also floating point variables. You can use a float if you want to, that's fine, I just usually avoid them and prefer to use doubles instead. Indeed the flag for float is %f

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. Cheque Printing Program Query
    By karlawarla in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2006, 02:11 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM