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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C scanf problem

  • 06-12-2004 4:14pm
    #1
    Registered Users, Registered Users 2 Posts: 7,893 ✭✭✭


    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, Registered Users 2 Posts: 16,414 ✭✭✭✭Trojan




  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 7,893 ✭✭✭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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 7,278 ✭✭✭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