Just a note, there is another question on Stack similar to mine, but that question actually asks two questions. One of the questions is unrelated to my question, and it's the only question answered.
I also ask a different question. It can be found below: What am I suppose to do with the zero stored in the array through the pointer?
The exercise gives me a function, int getint(int *)
, which takes the address of an array element, converts ASCII digits from the input stream, and then stores them as a decimal number in the int pointer. It returns EOF
for end of file, zero if the next input is not a number, and a positive value if the input contains a valid number. The function contains a function ungetch()
which pushes a character back onto the input stream.
I don't understand the wording of the following exercise:
As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such character back on the input
Is this saying I should push the + or - back on to the input stream or push the character that wasn't a number back onto the input stream? Also, how should I treat the zero stored in the array through the pointer?
Here is the code:
int getint(int *pn)
{
int c, sign;
while (isspace(c = getch()))
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-'){
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
c = getch();
for (*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if (c != EOF)
ungetch(c);
return c;
}