-1

I don't know where the problem lies. The code compiles without any error, but while running it, it asks me for a number but when I hit enter after giving an input it crashes. Can anybody tell me where the problem lies? I think there is some memory problems but I can't figure out where. Thanks in advance.

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

int main(void)
{ 
    int n[16];
    long long a;
    printf("enter your credit card number \n");
    scanf(" %lld", &a);
    int i;
    for ( i = 0; i < 15; i++)
    {
        n[i] = (a % (10^(15-i))) / 10^(14-i);
    }
    int m = (n[1] + n[3] + n[5] + n[7] + n[9] + n[11] + n[13]);
int w = 2 * m;
int k = w % 10;
int l = n[0] + n[2] + n[4] + n[6] + n[8] + n[10] + n[12] + n[14];
int z = k + l;
if (z % 10 == 0)
{
    printf("you card is valid");
}
else
{
    printf("go get a new card");
}
return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Salmaan
  • 31
  • 8
  • Please don't spam with language tag. Only add the relevant tag. – Some programmer dude Dec 21 '16 at 13:17
  • Possible Overflow? – Sourav Ghosh Dec 21 '16 at 13:18
  • As for your problem, first of all you should run a debug-build in a debugger to locate where in your code the crash happens. Then examine the involved variables to see if they are all looking okay. Finally edit your question to include those details. – Some programmer dude Dec 21 '16 at 13:18
  • 3
    Oh, and you *do* know that the `^` operator is the *bitwise xor* operator and not exponentiation? – Some programmer dude Dec 21 '16 at 13:19
  • `scanf(" %lld", &a);` why extra space? – Azodious Dec 21 '16 at 13:24
  • Possible duplicate of [Conditional check for "i == (2^8)" fails when i is 512?](http://stackoverflow.com/questions/11607854/conditional-check-for-i-28-fails-when-i-is-512) – Martin R Dec 21 '16 at 13:30
  • Possible duplicate of [mathematical power operator not working as expected](http://stackoverflow.com/questions/2027877/mathematical-power-operator-not-working-as-expected). – Martin R Dec 21 '16 at 13:31
  • Possible duplicate of [Why is my power operator (^) not working?](http://stackoverflow.com/questions/4843304/why-is-my-power-operator-not-working/25108773#25108773). – Martin R Dec 21 '16 at 13:34

2 Answers2

2

^ means bit-wise XOR. If you want to take the power of something, use the pow() function.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • after using the pow() function although it does not crash but it does show every digit = 0. why is it so? – Salmaan Dec 23 '16 at 21:48
0

Though Lundin mentioned the desired code but the actual problem for your run-time error is this:

  • (10^(15-i)) gives 0 when i = 5 (10^10 remember the XOR rule?)

  • so (a % (10^(15-i))) becomes a%0 which is classical divide by zero run-time error.

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • It is worth mentoning that it does not crash on `i = 4` at `/ 10^(14-i)`, because `/` has a higher precedence than `^`. – mch Dec 21 '16 at 13:49