0

I am making a checksum error verifying program using C language but I have encountered an infinite loop while executing the program and I don't have any clue why I am getting this.

So what I have done in this program is that I am going to first convert characters of a string into respective ASCII codes and then checking for checksum error and for this I have made this program below.

#include <stdio.h>
int a[2], i, dec[10][8], de[2][8], j, k, add[8], carry = 0, n = 5;
int u[] = {0, 0, 0, 0, 0, 0, 0, 1};
char b[] = {'H', 'e', 'l', 'l', 'o'};
int main()
{
    for (k = 0; k < 2; k++)
    {
        for (j = 0; j < 8; j++)
        {
            dec[k][j] = 0;
        }
    }
    for (k = 0; k < 2; k++)
    {
        a[k] = b[k];
    }
    for (k = 0; k < 2; k++)
    {
        i = 0;
        while (a[k] > 0)
        {
            dec[k][i] = a[k] % 2;
            a[k] = a[k] / 2;
            i++;
        }
    }
    i = 0;
s:
    for (k = 0; k < 2; k++)
    {
        for (j = 7; j >= 0; j--)
        {
            de[k][i] = dec[k][j];
            i++;
        }
        i = 0;
    }

    for (k = 0; k < 2; k++)
    {
        for (j = 0; j < 8; j++)
        {
            printf("%d", de[k][j]);
        }
        printf("\n");
    }
    i = 0;
    for (j = 7; j >= 0; j--)
    {
        if (de[i][j] == 1 && de[i + 1][j] == 1 && carry == 0)
        {
            add[j] = 0;
            carry = 1;
        }
        else if (de[i][j] == 1 && de[i + 1][j] == 1 && carry == 1)
        {
            add[j] = 1;
            carry = 1;
        }
        else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 1)
        {
            add[j] = 0;
            carry = 1;
        }
        else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 0)
        {
            add[j] = 1;
            carry = 0;
        }
        else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 1)
        {
            add[j] = 0;
            carry = 1;
        }
        else if (de[i + 1][j] == 0 && de[i][j] == 1 && carry == 0)
        {
            add[j] = 1;
            carry = 0;
        }
        else if (de[i + 1][j] == 0 && de[i][j] == 0 && carry == 0)
        {
            add[j] = 0;
            carry = 0;
        }
        else if (de[i + 1][j] == 0 && de[i][j] == 0 && carry == 1)
        {
            add[j] = 1;
            carry = 0;
        }
    }
    i = i + 2;
    for (k = 0; k < 8; k++)
    {
        de[i][j] = add[j];
    }
    if (carry == 1 && i < n)
    {
        for (k = 0; k < 8; k++)
        {
            de[i + 1][j] = u[j];
        }
        goto s;
    }

    else if (carry == 0 && i < n)
    {
        for (k = 0; k < 8; k++)
        {
            de[i + 1][j] = u[j];
        }
        goto s;
    }
    for (i = 0; i < 8; i++)
        printf("%d", add[i]);
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    That's a lot of code to look at. Which loop is the problem? – DeiDei Feb 09 '20 at 04:12
  • 1
    The label `s:` and the two `goto s;` statements are surprising. I don't like the idea of working out what those do. Have you stepped through in the debugger to see where the infinite loop is occurring? There's a lot of interesting (odd, maybe even weird) stuff in the initialization loops and array sizes. It isn't clear what checksum algorithm you're implementing. It isn't even particularly clear what you are calculating the checksum of — I suppose it is `b`, but the first loop using `b` only looks at the first two characters. – Jonathan Leffler Feb 09 '20 at 04:20
  • 1
    Is a lot of code, but I think the problem is that one of the 2 `goto s;` are always executing. That is because they are in an `if` with `i < n` and `i` is always setting as 0 several lines before that – Eduardo Pascual Aseff Feb 09 '20 at 04:21
  • 1
    I think you're probably right, @EduardoPascualAseff. Although there's also an `i = i + 2;` after the `i = 0;`, since `n == 5`, `i` is always less than `n`. It goes to confirm prejudices against labels and `goto` statements forming loops. Using `goto` to exit from inside a loop is one thing — making ad hoc loops is very much another thing and generally a bad idea. – Jonathan Leffler Feb 09 '20 at 04:26

0 Answers0