1

Tasked with writing a small program for an intro programming course where "#"s are output in the terminal depending on the integers input, executed when EOF character is input. It looks terrible but it works fine except when a single 1 or 0 is input, then a "D" is output immediately after desired output, and I have no idea why or how it could be possible.

I've tried wiggling around in the array used to store input for execution and there are no "D"s in there. There are no "D"s anywhere. It doesn't happen with any other inputs so far.

int main()
{
    int n = 0;
    int index = 0;
    int field[2000] = {};
    int length = 0;
    while (1)
    {
        int check = scanf("%d", &n);
        if (check == EOF)
        {
                break;
        }
        field[index] = n;
        index += 1;
        length += 1;
    }
    for (int i = 0; i < length ; i++)
    {
        if (field[i] == 0)
        {
            printf(" ");
        }
        else
        {
            for(int j = 0; j < field[i]; j++)
            {
                printf("#");
            }
        }
        printf("\n");
    }
    return 0;
}

I expected a single "#" to be output or a space, no other characters under any circumstances.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 6
    Are you sure that D isn’t part of the control sequence `^D` that’s printed on many terminals when the input sequence is manually terminated? – templatetypedef May 25 '19 at 22:29
  • This doesn't seem reproducible; https://code.sololearn.com/c7BnW73H7LV8/#c perhaps this has something to do with the way your inputs are being fed in? – Claies May 25 '19 at 22:39
  • " It looks terrible but it works fine except when a single 1 or 0 is input," lacks info. Post the exact key strokes used. – chux - Reinstate Monica May 25 '19 at 22:46
  • @templatetypedef I hadn't thought of that. A manual termination is what the program requires for the course which is also why I guess it couldn't be reproduced. We have a marking bot that marked this incorrect and the only thing I could think of as being incorrect was the D being printed. – Trevor Bonas May 25 '19 at 22:50
  • 1
    @chux Program run, integer '1' input, enter pressed, ctrl+d pressed for EOF. I do see now that the output problem is probably ^D output meaning manual termination. – Trevor Bonas May 25 '19 at 22:55
  • @TrevorBonas - not exactly sure I understand the requirement... do you mean when the input it `1` or `0`, output should be `#D` and `D` respectively? what if the input is `10` or `01` what should the output be? – runwuf May 25 '19 at 22:56
  • @templatetypedef However D isn't printed with any other inputs. – Trevor Bonas May 25 '19 at 22:56
  • There are only three characters printed, the `' '` and `'#'` and `'\n'` all literals so how can a `'D'` be output? Are you sure it is not the echoed input? – Weather Vane May 25 '19 at 22:57
  • @runwuf Input: <10> output: <##########> Input: <4 5 10> output: <####\n#####\n##########> input: <1> (or <01>) output (what it's supposed to do): <#> input: <0> output (what it's supposed to do): < > – Trevor Bonas May 25 '19 at 23:02
  • @TrevorBonas your code works for me, I can't reproduce it... as other suggested, something with how `^D` is feed in? or something weird with the marking bot? it's probably using a input file to redirect the input? do you have access to that input text file? – runwuf May 25 '19 at 23:22
  • "However D isn't printed with any other inputs" --> it is over written by longer code output. Add a `printf("\n");` before `for (int i = 0; i < length ; i++)` and I suspect you will see it every-time. – chux - Reinstate Monica May 26 '19 at 03:03
  • `stty -echoctl` should fix the problem — you turn it back on with `stty echoctl` if you decide you don't like it. – Jonathan Leffler May 27 '19 at 05:10
  • See [Why does C program print `0D` instead of `0`? (When EOF sent as Ctrl+D)](https://stackoverflow.com/questions/8306225/) — I'd close this as a duplicate if I hadn't already used my close vote for another reason. – Jonathan Leffler May 27 '19 at 05:13
  • What other things could be incorrect? Understanding of rhe requirements. – n. m. could be an AI Dec 15 '21 at 05:57
  • `int field[2000] = {};` isn't valid C. – Lundin Dec 15 '21 at 07:21

0 Answers0