Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C scanf problem

Options
  • 06-12-2004 5:14pm
    #1
    Registered Users Posts: 7,868 ✭✭✭


    im getting an error with scanf("%.2f", &fAmount) ; so i just use scanf("%f", &fAmount) ; . however, this is causing me a problem.

    i need to scan in a monetary value. if i just type in, say, 150.95 it will give me 150.949997. does anyone know why this is? im using borland C++ builder 4.


Comments

  • Registered Users Posts: 16,413 ✭✭✭✭Trojan




  • Registered Users Posts: 568 ✭✭✭phil


    There's plenty of explanations on the web regarding floating point arithmetic, where "approximations" of numbers are basically taken in order to minimize computation time.

    In cases where you need to be ACCURATE (In programming the definition between accuracy and precision is important), using floating point arithmetic instead of fixed point is normally a bad idea.

    This is normally the case when dealing with currency/money.

    Phil.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    The_B_Man wrote:
    im getting an error with scanf("%.2f", &fAmount) ; so i just use scanf("%f", &fAmount) ;
    What you're doing is impossible :P What you want to do is just to use scanf as you are currently using, and when you run your printf command, you stick in the .02 there, and truncate the value only when you're printing the final result.


  • Registered Users Posts: 7,868 ✭✭✭The_B_Man


    no coz the problem arises when im evaluating the integer! i'm not outputting that particular integer. i need to find out how many pennies are in it and if im gettin 150.949997 instead of 150.95 its a problem coz it'll be one penny off. so in essence, i need to round the number off, but im actually putting in a user value of 150.95 and for whatever reason, its giving me the 150.949997 or watever.


  • Registered Users Posts: 2,660 ✭✭✭Baz_


    I think you'll find its because you didn't RTFM...

    This is so basic, and probably answered billions of times on various webpages that you are just being ridiculously lazy, not to mention you probably didn't even read your notes...


  • Advertisement
  • Registered Users Posts: 2,426 ✭✭✭ressem


    You've a choice.
    If you need the computer to keep the number of pennies exactly then

    1. Ask for the amount in pennies. Store as int, long or one of the other non floating point types. Since this is homework, this probably is not an option.


    2. take the input as a char array.
    Use string manipulation to get the number above and below the decimal point.
    Multiply above part by 100, add and store as value in pennies.
    Requires checking that number of penny digits is exactly 2.
    Probably a bit ahead of you yet.


    3. Store as floating point. Multiply by 100 and round (not truncate) to get the number of pennies.
    Rounding can bring 94.9 to 95, as your notes or google will show you.


    Academic bit, that you need to know for any real work ( and exams).
    Floating point numbers are stored in a form that looks like scientific notation in binary. e.g
    1.1110111 X 2^8
    so numbers that multiples of 2^n may not be stored perfectly in floating point.
    1.95 = 1 + .5 + .25 + .125 + .0625 + 0.0078125 + 0.00390625 + ....
    in binary = 1 + .1 + .01 + .001 +.0001 + .0000001 + .00000001 + ...

    You have to assume that they won't be held exactly, look at what operations you need to carry out on such numbers, choose whether you need the improved precision of a double, and carry out rounds or truncations when you need to display numbers, preferably on a copy of the number, if you'll be using it again in your program.


  • Registered Users Posts: 7,276 ✭✭✭kenmc


    you could do something like this....

    int maj=0,min=0;
    scanf("%d.%d", &maj, &min);

    then process the 2 values into one....


Advertisement