2021年1月25日星期一

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 value, array[7], gives random numbers such 4199280, or other 7 digit number. 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);  }  
https://stackoverflow.com/questions/65894935/array-giving-random-numbers-at-the-end-of-array7 January 26, 2021 at 09:59AM

没有评论:

发表评论