These are the test cases which my code doesn't satisfy ,can someone please help me out as i have absolutely no idea why it is happening.

``````:( identifies 378282246310005 as AMEX
expected "AMEX\n", not "INVALID\n"

:( identifies 5555555555554444 as MASTERCARD
expected "MASTERCARD\n", not "INVALID\n"

:( identifies 4012888888881881 as VISA
expected "VISA\n", not "INVALID\n"
``````
``````#include <cs50.h>
#include <stdio.h>

// program to check whether card is valid or not,and to check which type of card.

int main(void)
{
long cc = get_long("NUMBER : ");    //taking user input
long p1 = cc;
long p2 = cc/10;
int a, b, c, d, sum1 = 0, sum2 = 0;

do                                  // check sum part  of program start
{
a = p1%10;
sum1 = sum1 + a;
p1 = p1/100;
}
while(p1!=0);

do
{
b = p2%10;
if( (2*b) > 9)
{
d = (2*b)%10;
c = c + d;
d = 2*b/10;
c = c + d;
}
else
{
c = 2*b;
}
sum2 = sum2 + c;
p2 = p2/100;
}
while(p2!=0);

int lstdgt = (sum1 + sum2)%10;
int ae = cc/10000000000000;
int mc = cc/100000000000000;
int visa1 = cc/1000000000000000;
int visa2 = cc/1000000000000;

if(lstdgt == 0)                     //giving types of card depending on the card number.
{
if( (ae == 35) || (ae == 37) )
{
printf("AMEX\n");
}
else if( (mc == 51) || (mc == 52) || (mc == 53) || (mc == 54) || (mc == 55) )
{
printf("MASTERCARD\n");
}
else if( (visa1 == 4) || (visa2 == 4) )
{
printf("VISA\n");
}
}
else
{
printf("INVALID\n");
}
}
``````

## tl;dr

Initialize `c` to zero inside of the do/while loop and before line 27 `c = c + d;` happens.

## Explanation

The problem lies in line 27. This is when you have the case that one of the digits multiplied by 2 is a number greater than 9 so that you have to add up each digit.

This is the block of code:

``````    if( (2*b) > 9)
{
d = (2*b)%10;
c = c + d;
d = 2*b/10;
c = c + d;
}
``````

At this point in the code `c` contains the value of the last digit multiplied by two. I think you meant to initialize `c` to zero before doing something like `c = c + d;`

Also it is a good practice to initialize to zero all variables that are going to be used to store sums of numbers. In the case of `a`, `b`, and `d`, the first time they are used in the code they get initialized to values that do not depend on themselves, so that is good. But in the case of `c` there are 2 scenarios the first time it gets assigned a value because the value depends on `c` itself:

Line 27:

``````c = c + d;
``````

In this case the value of `c` could be one of two things:

1. Garbage value (which could really be anything). This would happen if line 33 `c = 2*b;` has not happened yet.
2. Or if line 33 has already been executed, then `c` is whatever was leftover from the last iteration.