C Program not running as intended, hangs after input

Posted by user41419 on Stack Overflow See other posts from Stack Overflow or by user41419
Published on 2012-11-28T04:55:31Z Indexed on 2012/11/28 5:04 UTC
Read the original article Hit count: 145

Filed under:
|
|

The program I am writing to take a number and display that number as a calculator would display it (shown below) is compiling with no issues, but when I try to run it, I am able to input my number, but nothing happens. It seems like it is "hanging", since no further output is shown as I would have expected. Might anyone know what the problem is?

#include <stdio.h>
#define MAX_DIGITS 20

char segments[10][7] =              /* seven segment array */
    {{'1','1','1','1','1','1','0'}, /* zero */
     {'0','1','1','0','0','0','0'}, /* one */
     {'1','1','0','1','1','0','1'}, /* two */
     {'1','1','1','1','0','0','1'}, /* three */
     {'0','1','1','0','0','1','1'}, /* four */
     {'1','0','1','1','0','1','1'}, /* five */
     {'1','0','1','1','1','1','1'}, /* six */
     {'1','1','1','0','0','0','0'}, /* seven */
     {'1','1','1','1','1','1','1'}, /* eight */
     {'1','1','1','0','0','1','1'}};/* nine */
char digits[3][MAX_DIGITS * 4];     /* digits array */
int i, j;                           /* count variables */
int adjust;                         /* output formatting */

int main(void) {
    clear_digits_array();

    int digit[20];
    for (i = 0; i < 20; i++) {
        digit[i] = 0;
    }

    int count = 20;
    int position = 0;

    printf("Enter a number: ");

    int number = scanf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
            &digit[0],
            &digit[1],
            &digit[2],
            &digit[3],
            &digit[4],
            &digit[5],
            &digit[6],
            &digit[7],
            &digit[8],
            &digit[9],
            &digit[10],
            &digit[11],
            &digit[12],
            &digit[13],
            &digit[14],
            &digit[15],
            &digit[16],
            &digit[17],
            &digit[18],
            &digit[19]);                        //NOTHING HAPPENS AFTER HERE

    printf("Got input, number is %d", number); 

    while (count > 0) {
        printf("Reading digits, count is %d", count);
        process_digit(digit[20 - count], position);
        position++;
        count--;
    }

    print_digits_array();
    printf("\n");
    return 0;
}

void clear_digits_array(void) {
    /* fill all positions in digits array with blank spaces */
    for (i = 0; i < 3; i++) {
        for (j = 0; j < (MAX_DIGITS * 4); j++) {
            digits[i][j] = ' ';
        }
    }
}

void process_digit(int digit, int position) {
    /* check each segment to see if segment should be filled in for given digit */
    for (i = 0; i < 7; i++) {
        printf("Processing digit %d at position %d, i is %d", digit, position, i);
        if (segments[digit][i] == 1) {
            switch (i) {
                case 0: digits[0][(position * 4) + 1] = '_';
                        break;
                case 1: digits[1][(position * 4) + 2] = '|';
                        break;
                case 2: digits[2][(position * 4) + 2] = '|';
                        break;
                case 3: digits[2][(position * 4) + 1] = '_';
                        break;
                case 4: digits[2][(position * 4) + 0] = '|';
                        break;
                case 5: digits[1][(position * 4) + 0] = '|';
                        break;
                case 6: digits[1][(position * 4) + 1] = '_';
                        break;
            }
        }
    }
}

void print_digits_array(void) {
    /* print each character in digits array */
    for (i = 0; i < 3; i++) {
        for (j = 0; j < (MAX_DIGITS * 4); j++) {
            printf("%c", digits[i][j]);
        }
        printf("/n");
    }
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about arrays