#define MAXBUF 1000
int buf[MAXBUF];
int buffered = 0;
int bufp = 0;
int getch()
{
if(bufp > 0) {
if(!--bufp)
buffered = 0;
return buf[bufp];
}
else {
buffered = 0;
return getchar();
}
}
void ungetch(int c)
{
buf[bufp++] = c;
buffered = 1;
}
int getfloat(float *pn)
{
int c, sign, sawsign;
float power = 1.0;
while(isspace(c=getch()))
;
if(!isdigit(c) && c!= '+' && c!= '-' && c != '.') {
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if(sawsign = (c == '-' || c == '+'))
c = getch();
if(c != '.' && !isdigit(c)) {
ungetch(c);
if(sawsign)
ungetch((sign == -1) ? '-' : '+');
return 0;
}
for(*pn = 0.0; isdigit(c); c = getch())
*pn = 10.0 * *pn + (float)(c - '0');
if(c == '.')
while(isdigit(c = getch())) {
*pn = 10.0 * *pn + (float)(c - '0');
power *= 10.0;
}
*pn *= sign;
*pn /= power;
ungetch(c);
return c;
}
It always returns 23.7999 when i enter 23.8, and i have no idea why. Can anybody tell me why?