2

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;
}
Spellbinder2050
  • 644
  • 3
  • 13
  • 1
    The problem with K&R is that encourages one to write code like the above. – Oliver Charlesworth Sep 01 '14 at 18:46
  • What's wrong with it? What would you have me do? I'm just using the book to learn to code and the language. – Spellbinder2050 Sep 01 '14 at 18:51
  • The issue is that that book is incredibly outdated, the language and its usage (in terms of best practice, etc.) have changed a lot since it was written. I strongly recommend learning from one of the "Beginner" books listed here: http://stackoverflow.com/a/562377/129570 – Oliver Charlesworth Sep 01 '14 at 18:53
  • Why hasn't anyone pointed this out to me? I've been writing questions concerning this book for a month already. I'm half way through the tutorial portion of the book. – Spellbinder2050 Sep 01 '14 at 18:57
  • possible duplicate of [getint and getch](http://stackoverflow.com/questions/1995283/getint-and-getch) – n. m. could be an AI Sep 01 '14 at 19:02
  • Don't worry, K&R is still a good book about C, even if somewhat outdated. You can catch up with modern developments later. Why learn programming by doing exercises in C in 2014 is a different question. – n. m. could be an AI Sep 01 '14 at 19:02
  • I'm planning to jump to assembly next to understand the machine better, simultaneously with objective-c to get some App development in. I've gone through a lot of this book before, so I figured I could just go back through it. – Spellbinder2050 Sep 01 '14 at 19:18

1 Answers1

3

The exercise is asking you to recognize that there's a subtle bug in the program when you encounter a '+' or '-' but what follows is not a number.

E.g.,

+<EOF>
+apple
-banana

As opposed to actual numbers, like

+1
-17

In the current program, if you see the former case in the data stream, you are consuming the '+' and '-', and then telling the user that you saw a number in the stream and that number was 0. But that's not true at all!

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?

Think about what you would expect as a consumer of this function. Like let's say a co-worker gave you this function and you had to use it. What would you want to happen? There are really 2 choices:

  1. Consume the '+' or '-' from the data stream and discard it. Tell the caller with the return value you did not see a number.

  2. Put the '+' or '-' back into the data stream. Tell the caller that you did not see a number.

Chances are, you'd want the function not to throw away data that it did not use. What if you passed a string to the function with this text:

++var;

Is it OK to consume the '+' and just leave +var;? I don't think so.

So to answer your question directly, push the '+' or '-' back into the stream. Make sure you have put all characters back in the stream that you took out. Return to the user the code that indicates what follows in the stream is not a number.

Also, how should I treat the zero stored in the array through the pointer?

You don't! If you did not see a number, why are you telling the caller that you saw the number 0? Change the code so that you don't set '0' in the array if you did not see a number.

(This one is a little more ambiguous. You could document the function such that the caller must treat pn as garbage if you returned EOF or 0. Then you could write whatever you wanted to pn as long as you simply returned 0. But it's a good exercise to figure out how to modify the function such that you don't touch pn at all if you don't need to.)

indiv
  • 17,306
  • 6
  • 61
  • 82