Q: array giving random numbers at the end of array[7] I am trying to correctly print the values inside the array. The program is supposed to take only numbers that are 13 digits or higher. If I give n a number of 13 digits or 14 digits, the last position of the long array[8], which is array[7], gives random numbers such 4199280, or other 7 digit number. how can set array[7] to 0 then, so it doesn't hinder in my multiplication later. Can someone help me fix what I might be doing wrong without knowing? Thanks in advance.
#include <cs50.h>
#include <stdio.h>
#include <math.h>

long countDigit(long long n);

int main(void)
{
    long n;

   //This is asking for the input
    do
    {
        n = get_long("Number: ");
    }
    while(!(countDigit(n)>12));

    //Checksum math
    long everyOther = 0;
    long array[8];
      int i = 0;


    while(n > 0)
    {

        long lastNumber = n/10;
        everyOther = lastNumber % 10;
        n = n / 100;
        printf("%li\n", everyOther);
        array[i] = everyOther;
        i++;

    }
    printf("\n");
    printf("%li\n", array[0]);
    printf("%li\n", array[1]);
    printf("%li\n", array[2]);
    printf("%li\n", array[3]);
    printf("%li\n", array[4]);
    printf("%li\n", array[5]);
    printf("%li\n", array[6]);
    printf("%li\n", array[7]);

     printf("\n");


    // long Multi = ((2*(array[0])) + (2*(array[1])) + (2*(array[2])) + (2*(array[3]))
    //                 + (2*(array[4])) + (2*(array[5])) + (2*(array[6])) + (2*(array[7])));
    // printf("%li\n", Multi);
}


//This function helps us with the counting of the number
long countDigit(long long n) {
  return floor(log10(n) + 1);
}


A: To see what is happening more clearly, try adding a printf to the loop just before or after setting array[i]:
printf("array[%d] = %li\n", i, everyOther);

this will show you exactly which elements of the array get set, and what they get set to.  You'll see, when the input is 13 or 14 digits, this will happen 7 times.  For example, if your input is 1234567890123, you'll see
array[0] = 2
array[1] = 0
array[2] = 8
array[3] = 6
array[4] = 4
array[5] = 2
array[6] = 0

thus showing that you're only setting those 7 values of the array.  So if you later try to print array[7], you'll get garbage; it has never been set to anything.
