C code
#define NUMBER '0'
#define TOOBIG '9'
getop (s, lim)
char s[];
int lim;
{
int i, c;
while ((c = getch()) == ' ' || c == '\t' || c == '\n');
if (c != '.' && (c < '0' || c > '9')) return (c);
s[0] = c;
for (i = 1; (c = getchar()) >= '0' && c <= '9'; i++)
if (i < lim) s = c;
if (c == '.'){ //collect fraction
if (i < lim) s = c;
for (i++; (c = getchar()) >= '0' && c <= '9'; i++)
if (i < lim) s = c;
}
if (i < lim){ //number is ok
ungetch (c);
s = '\0';
return NUMBER;
}
else { //it's too big, skip the rest of the line
while (c != '\n' && c != EOF) c = getchar();
s[lim-1] = '\0';
return TOOBIG;
}
}
C code
#define BUFSIZE 100
char buf[BUFSIZE]; //buffer for ungetch
int bufp = 0; //pointer to the next free position in buf[]
int getch()
{
return ((bufp > 0) ? buf[--bufp] : getchar());
}
ungetch (c)
int c;
{
if (bufp > BUFSIZE) printf ("ungetch error: too many characters\n");
else buf[bufp++] = c;
}
Exercise 4-6: This
getch and
ungetch do not handle a pushed-back EOF in a portable way. Why?
Decide what their properties ought to be if an EOF is pushed back, then implement your design.
My problem is the first part of this exercise. Why is a pushed-back EOF not handled properly?
OK, if EOF is a negative constant, you probably have to change the
char array to an
int array, but other than that.
P.S. Those s = c; are supposed to be s[ i ] = c; (without the spaces)
For some reason they don't appear as such :/
...and that's the bottom line because Mate de Vita said so.