开发者

Fill shape with asterisks

I wrote a program to fill in closed figures with asterisks.

For some reason it isn't accepting the sentinel value EOF Ctrl+D.

Why is this?

#include "usefunc.h"

#define height 100
#define width 100

void showRow(int numbers[], int size_numbers) {
    int i;
    printf("[ ");
    for (i = 0; i < size_numbers-3; i++) {
        printf("%c, ", numbers[i]);
  }
    printf("%c ]", numbers[size_numbers-3]);
    printf("\n");
}

void showshape(int shape[][width], int lines, int max_buf) {开发者_如何学运维
    int i, j;
    for (i = 0; i < lines; i++) {
        for (j = 0; j < max_buf; j++) {
            printf("%c", shape[i][j]);
        }
        printf("\n");
    }
}

void fill(int row[][width], int rownum, int end) {
    int i, c = 1, inside = 0;
    for (i = 0; i < end; i++) {
        if (row[rownum][i] == '*') {
            c++;
        }
        if (!(c%2)) inside = 1;
        else inside = 0;
        if (inside) {
            row[rownum][i] = '*';
        }
    }
}

int main () {
    int shape[height][width], i = 0, j = 0, lines = 0;
    int sentinel = 0;
    int temp = 0;
    while (sentinel != EOF) {
        while ((temp = getchar()) != '\n') {
            sentinel = temp;
            shape[i][j] = temp;
            j++;
        }
        i++;
        lines++;
    }
    for (i = 0; i < lines; i++) {
        fill(shape, i, width);
    }
    fill(shape, 0, j);
    //for (i = 0; i < lines; i++)
    showshape(shape, lines, j+2);
}

Edit 1

Just updated the code. It doesn't quite print the box. what's going on?


Edit 2

Another update. This time I'm copying the value of temp, however I get

Bus error 

What am i doing wrong?


You want:

int temp;

EOF is an integer value, not a char.


while ((temp = getchar()) != '\n') {
    shape`[i][j]` = temp;
    j++;
}

I suspect this this never exits once EOF is reached. I mean, getchar probably continues to throw EOF at you and you ask "Not \n ? Okay, no need to stop".

Also, what @Neil Butterworth said in his answer is really sensible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜