How to avoid stack smashing detected in C? What is wrong with the code below?

  Kiến thức lập trình

I’m making a program which automates Luhn’s algorithm; it takes every other number of a credit card starting with the first one, multiplies them by two, and adds the multiplied the added numbers together. I’ve not finished, but have encountered a problem called “stack smashing detected”. Can anybody offer help or advice?

Here is the code:

#include <stdio.h>
#include <math.h>

int main(void) {

  long card;
  do
    {
      printf("Enter the card number: ");
      scanf("%li", &card);
    }
  while (card < 1000000000000000);

  int y = 10;
  int z = 15;
  int x = 1;
  int arr[8];


  for(int i = 0; i < 8; i++)
    {
      double result = pow(y,z); 

      double a = (card / result); 
      long roundedA = floor(a);

      double b = roundedA /10;
      long roundedB = floor(b);
      long c = roundedB * 10;
      int d = roundedA - c;
      z = z-2;

      arr[x] = d;
      x++;

    }


  int newarr[8];
  int arraynumber;
  for(int j = 0; j < 7; j++)
    {
      arraynumber = 2 * arr[j];
      newarr[j] = arraynumber;
    }  

  int sum = 0;
  for(int k = 0; k < 7; k++)
    {
      sum = sum + newarr[k];
    }

  if (sum % 10 == 0)
  {
    printf("This checks out, boss.");
  }
  else
  {
    printf("Security will escort you out of the building big man.");
  }


}

I have no idea what this means, so I don’t know what to try.

New contributor

Vihaan Kaustuv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT