How can I filter out garbage values? (for a credit card validation program)

Viewed 34

I'm writing a code that checks for valid credit card numbers.

For this program, I'm following the Luhn algorithm. I was able to get the credit card number into an array Credit_No[] and passed it into a function called Sum1 where we should double the value of every second digit and find the sum of these doubled numbers. If the result of the doubling operation is a two-digit number, we should add the digits of the doubled number before finding the sum.

However, I checked my calculations (in Sum1) using some printf statements to see that there are some repeating garbage values. I'm fairly new to C so It would be really helpful if someone could explain why this happens. Thankyou!

code:

  #include<stdio.h>
  #include <string.h>
  #include <stdlib.h>


    void Sum1(int Credit_No[], int size);
    void Sum2(int Credit_No[], int size);

    int main() {
        char string[20];
        int integer[4];
        printf("Please enter your credit card number: ");
        scanf("%15[^\n]",string);
        // Extract the first token
        char * token = strtok(string," ");
        int y = atoi(token);
        integer[0] = y;

        int i=1;
        // loop through the string to extract all other tokens
        while(token != NULL ) 
        {
          printf( " %s\n", token ); //printing each token
          token = strtok(NULL," ");
          int x = atoi(token);
          integer[i] = x;            //storing tokens into an array
          i++;
        }
      integer[3] = -1;


     //First token - storing 4 digits seperately
     int value1 = integer[0];
     int value1_counter = 3;
     int value1_arr[4];
     while(value1 > 0)
     {
       int value1_rem = value1 % 10;
       value1_arr[value1_counter] = value1_rem;
       value1 = value1 / 10;
       value1_counter--;
     }


    //Second token - storing 4 digits seperately
      int value2 = integer[1];
      int value2_counter = 3;
      int value2_arr[4];
      while(value2 > 0)
      {
        int value2_rem = value2 % 10;
        value2_arr[value1_counter] = value2_rem;
        value2 = value2 / 10;
        value2_counter--;
      }


     //Third token - storing 4 digits seperately
      int value3 = integer[2];
      int value3_counter = 3;
      int value3_arr[4];
      while(value3 > 0)
       {
         int value3_rem = value3 % 10;
         value3_arr[value1_counter] = value3_rem;
         value3 = value3 / 10;
         value3_counter--;
       }

    int Credit_No[12]= {0};   //storing seperated digits from all 3 token into one array

Credit_No[0] = value1_arr[0];
Credit_No[1] = value1_arr[1];
Credit_No[2] = value1_arr[2];
Credit_No[3] = value1_arr[3];
Credit_No[4] = value2_arr[0];
Credit_No[5] = value2_arr[1];
Credit_No[6] = value2_arr[2];
Credit_No[7] = value2_arr[3];
Credit_No[8] = value3_arr[0];
Credit_No[9] = value3_arr[1];
Credit_No[10] = value3_arr[2];
Credit_No[11] = value3_arr[3];

Sum1(Credit_No, 12);
Sum2(Credit_No, 12);

   return 0;
}

 void Sum1(int Credit_No[], int size)
{


  int tot_sum=0;
  int Sum1=0;
  int sum_rem=0;
  int sum_div=0;
  int digit1=0;
  int digit2=0;

for(int count=1; count<13; count+=2)                    //traversing through the Credit_No array
{
    tot_sum = Credit_No[count] + Credit_No[count];      //doubling the value
    if (tot_sum < 10)                                   //To check if its a double digit number
    {
        Sum1 = Sum1 + tot_sum;
        printf("\nSum1 (if) = %d",Sum1);                //---->checking calculation
    }
    else
    {
        sum_rem = tot_sum % 10;                        //seperating the double digits by getting remainder and division whole number
        sum_div = tot_sum / 10;
        
        digit1 = sum_rem;                              //assigning remainder value to digit 1
        printf("\ndigit1 = %d",digit1);                //---->checking calculation
        
        digit2 = sum_div;                              //assigning division value to digit 2
        printf("\ndigit2 = %d",digit2);                //---->checking calculation
        
        Sum1 = Sum1 + digit1 + digit2;                // adding digit 1 and 2 together and adding to Sum1
        printf("\nSum1 (else) = %d",Sum1);            //---->checking calculation

    }
    
}
printf("\nSum1 = %d",Sum1);                           //---->checking calculation

}

Output I'm getting:

please enter you credit card number: 4223 3333 2431
4223
3333
2431
Sum1 (if) = 4
Sum1 (if) = 10
digit1 = 8
digit2 = 315944
Sum1 (else) = 315962
Sum1 (if) = -344739484
Sum1 (if) = -344739484
Sum1 (if) = -344739478
Sum1  = -344739478
Sum2 -> 4200707
0 Answers
Related