# getint that properly handles + or - signs

As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a charachter back on the input.

Before the for I check if the charachter after the + or - is a number. If it is, the function will store the representation of that number in *pn. If it is not, it will push back that characther back on the input - such that, everytime the getint is called it will read that charachter and push it back.

Here is my solution:

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();

if(!isdigit(c)) {
ungetch(c);
return 0;
}

for(*pn = 0; isdigit(c); (c = getch()))
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if(c != EOF)
ungetch(c);
return c;
}


2. It is unclear what the return value of the method means just by reading the code. If it is supposed to indicate success (return is not 0) or failure (return value is 0) then I think you have subtle bug in there in the case when the last character read from input is '\0' - in which case you return 0 even though you potentially have parsed a number. You should simply return 1; to indicate the success case.