string - C's strtod() returns wrong values -
i'm working on banking terminal program laboratory exercises @ university.
what puts me off stride function supposed take user's input of amount transfered, check if fits requirements , if so, returns value provided program.
our tutor mad means of securing input, , have call error on kind of inconsistency. value high? error. negative or 0 value? error. number more precise 0.01? error. non-digit-non-dot characters in input? error.
due that, function definately overcomplicated, yet i'm fine that. drives me wall fact, both atof()
, strtod()
functions reads numbers in somehow wrong way.
long double take_amount() { char input[21]; bool success, correct; unsigned long int control1; long double amount, control, control2, control3; { success = true, correct = true; printf("\t\tamount:\t\t"); success = scanf("%20[^ \t\n]c", input); __fpurge(stdin); correct = check(input, 'b'); amount = strtod(input, null); printf("\n\tgot %.20lf\n", amount); /// control = amount * 100; control1 = (unsigned long int)floor(control); control2 = (long double) control1; control3 = control2 - control; printf("\n\tgot2 %.20lf\n", control3); /// if (control3 != 0) { if (control3 >= 1 || control3 <= -1) { printf("\n\t\twe sorry, safety reasons impossible transfer"); printf("\n\t\tsuch great amounts while online. if wish finalize"); printf("\n\t\tthis operation, please, visit closest division of our bank."); printf("\n\t\twe sory inconvenience , wish pleasent day."); press_enter(); } correct = false; printf("\ndamn\n"); /// } if (amount <= 0) { correct = false; printf("\ngod damn\n"); /// } if(success == false || correct == false) { printf("\n\t\tinvalid input, please try again.\n\n"); } else { printf("\t\t\t\t%.2lf\n", amount); printf("\n\t\tis correct input? ((y)es/(n)o/(e)xit)"); if (accept()) { break; } else { continue; } break; } }while (1); return amount;
as far functions used here goes, check()
checks if string contains legal characters (digits , dot), press_enter()
waits enter-press leave main menu , accept()
reads y/n/e, return true
on y, false
on n , leaves menu on e.
the lengthy part control variables solution checking if number not more precise 0.01. sadly, doesn't work due strtod()
.
my problem strtod()
doesn't work! medicore numbers, being far underflow or overflow, value returned doesn't match input. examples:
enter amount deposit. amount: 1234.45 got 1234.45000000000004547474 enter amount deposit. amount: 0.999 got 0.99899999999999999911
it not unlikely it's fault, after several hours code still couldn't come working solution , that's why i'm asking help, vise internauts of stack overflow.
is there way fix reading of strtod()
? if not, there way take input let's me check needs checked?
edit: attention, please!
if haven't stated already, tutor not easiest guy work with, now.
forces use double format hold balance stored on accounts. know because 1 of collegues got program rejected using two-int, dollar - cent construction.
i highly appreciate here, can somehow solve problem? have use double type store money. have check if there no letters in input (scanf()
set on %lf
cut non-digits end), have check if input not more precise 0.01, have accept xxx.xx structure of input. suggestions?
use strtol
read dollars , again (or own trivial function) read cents, combine them cents += 100*dollars;
not use floating point currency. ever.
Comments
Post a Comment