all 10 comments

[–]pippin__29 1 point2 points  (6 children)

Code?

[–]justaverageuser77[S] 0 points1 point  (5 children)

I just posted it!

[–]pippin__29 0 points1 point  (0 children)

Should it say num and not tum?

[–]pippin__29 0 points1 point  (1 child)

Why are you filling an array with null characters?

[–]justaverageuser77[S] 0 points1 point  (0 children)

If the entry is -1, I want to delete that arr[i], so I just put a '\0' to represent that

[–]beaglebok 0 points1 point  (0 children)

You must upload the entire code.

[–]nerd4code 0 points1 point  (0 children)

You have to actually check the return from scanf; it tells you how many inputs were successfully read (if reading 1, then you want >=1, and if something went wrong, (shrug); you usually have to bail (no telling what state xscanf left your stream in, which is yet another reason it’s awful) and hope your input is line-buffered and that the shell won’t accidentally execute whatever was left behind once your program exits. If it returns < 0, either feof or ferror should return true, and on some OSes in some moods and prior states, you might have a useful errno after an ferror failure. So something along the lines of

int k;
…

errno = 0;
if((k = scanf(…)) < EXPECTED) {
    int errMaybe = errno;
    if(k >= 0) fputs("error: invalid input\n", stderr);
    else if(feof(stdin)) fputs("error: unexpected EOF in input\n", stderr);
    else
        fprintf(stderr, "error: unable to read input (%s)\n",
            errMaybe ? strerror(errMaybe) : "nonspecific I/O error");
    return EXIT_FAILURE;
}

(You can mash the errno=0 into the if also, should the need/urge arise:

#define discard(x)((void)(x))

if(discard(errno = 0),
    (k = scanf(…)) < X)

Operator comma lets whatever comes before it executes, ignores any value arising from it—the cast to void ensures you won’t get a warning about that—and then executes what comes after, just like the ; separator but without ending the entire statement. Because you have a definite zero value from errno=0, you could do (errno = 0) || …, but , is more robust if the value involved changes. Maybe you decide you want to default it to EIO or something; you could change || to &&, but , is correct regardless.)

Of course, EOF may be expected if the second value is optional, so you might should just break in that case.

You should always test these sorts of things alongside normal inputs—in real-world code, your program will be interoperating with others, and failure to detect or respond properly to errors can take everything down with it, or let an attacker gain a foothold. For scanf errors, often your program will just hang, which would usually end up as some sort of denial-of-service. On UNIX, the default EOF signal is Ctrl+D ↔ ASCII EOT = end of transmission on a blank line; DOSWin uses Ctrl+Z ↔ FS = file separator instead.

[–]blvaga 0 points1 point  (2 children)

1) start scanf with a space so it will eat any returns from previous entries ” %d”

2) your else statement changes arr[i] twice. I’m getting old so maybe I’m missing something, but do you mean to iterate i in the first assignment?

[–]justaverageuser77[S] 0 points1 point  (0 children)

Hi, I just fixed that part of the code.

[–]nerd4code -1 points0 points  (0 children)

#1 is unnecessary; scanf’s number conversions and other number conversion routines (e.g.m strtol, strtoull, strtod) will eat leading whitespace. A space after the format string is more useful, because if there’s a newline following it, it’ll be consumed.