0

In the find.c there is nothing like printf ("Retry: ") if user fails to insert a number, but it does. In the explanation of problem set it also states ctlr-d will cause to straw==INT_MAX and should immediately break the program, but it prints " Needle not found" before doing it as it is entered last else block.

1

Look at the implementation of GetInt() from http://cdn.cs50.net/2011/fall/lectures/5/src5/cs50.c.

/*
 * Reads a line of text from standard input and returns it as an
 * int in the range of [-2^31 + 1, 2^31 - 2], if possible; if text
 * does not represent such an int, user is prompted to retry.  Leading
 * and trailing whitespace is ignored.  For simplicity, overflow is not
 * detected.  If line can't be read, returns INT_MAX.
 */

int
GetInt(void)
{
    // try to get an int from user
    while (true)
    {
        // get line of text, returning INT_MAX on failure
        string line = GetString();
        if (line == NULL)
            return INT_MAX;

        // return an int if only an int (possibly with
        // leading and/or trailing whitespace) was provided
        int n; char c;
        if (sscanf(line, " %d %c", &n, &c) == 1)
        {
            free(line);
            return n;
        }
        else
        {
            free(line);
            printf("Retry: ");
        }
    }
}

If you are using GetInt() that's where the Retry: comes from.

About the INT_MAX not breaking your program, that's your job. GetInt() will return INT_MAX if Ctrl+D is pressed, but you should check if the returned value is INT_MAX, and if it is, break your loop.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .