I have started taking the CS5 lessons and while I run my code for the week-1 credit problem, the terminal always shows the output as "INVALID" or anything that I insert in the second last printf command. Can someone help me find the error?
Thanks
#include <cs50.h>
#include <stdio.h>
int main(void)
{
//getting the credit card number
long long cn=0;
do
{
cn=get_long_long("Enter your credit card number for validity check: ");
}
while(cn<1);
//determining the length of the credit card number
int length=0;
long long cn_temp=cn;
while(cn_temp>0)
{
cn_temp=cn_temp/10;
length++;
}
printf("The number of digits entered is: %i\n" , length);
//Storing the digits of the number in an array
int digits[length];
for(int i=0; i<length; i++)
{
digits[i]=cn%10;
cn/=10;
}
//Sum of odd and even places from the right
int sum_even=0;
for(int i=length-2; i>=0; i=i-2)
{
int even_places=digits[i]*2;
sum_even+=(even_places%10)+(even_places/10);
}
printf("Sum of even places from the right are: %i\n", sum_even);
//adding the alernate sum to sum of odd places integers
int sum_odd=0;
for(int i=length-1; i>=0; i=i-2)
{
int odd_places=digits[i];
sum_odd+=odd_places;
}
printf("Sum of odd places from the right are: %i\n", sum_odd);
// Sum of even and odd places digits
int sum_total=sum_even+sum_odd;
int remainder=sum_total%10;
if(remainder==0)
{
if(length==15 && digits[0]==3 && (digits[1]==4 ||digits[1]==7))
{
printf("AMEX\n");
return 0;
}
else if(length==16 && digits[0]==5 && (digits[1]==1 ||digits[1]==2||digits[1]==3||digits[1]==4||digits[1]==5))
{
printf("MASTERCARD\n");
return 0;
}
else if((length==13 || length==16) && digits[0]==4)
{
printf("VISA\n");
return 0;
}
else
{
printf("INVALID\n");
return 0;
}
}
else
{
printf("INVALID\n");
return 0;
}
}