# Can anyone help me troubleshoot my code for the CS50 Credit problem? (**not trying to cheat, just need some help!)

This is the code I have written in c for the cs50 credit pset 1 problem.

I am checking the length of numbers entered, and the first digits (AMEX 15 digits, starts with 34 or 37; MC 16 digits, starts with 51/52/53/54/55; VISA, 13 or 16 digits, starts with 4) and then applying Luhn's algorithm to see if a card is valid (every other digit x 2, add the digits of those multiplications together, and then add to the total of the digits not x2. If = 20, card is valid).

The trouble I am having is with the Luhn's algorithm section; there keeps being errors, and every time I try to fix these, others appear.

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

int main(void)
{
long num = get_long("Number: ");
long number = num;
int digit;
int x;
int total_sum;
int num_digits = 0;
do
{
num_digits ++;
num /= 10;
}
while (num!=0);
//invalid if not the right number of digits
if ((num_digits != 13) && (num_digits != 15) && (num_digits != 16))
{
printf("INVALID\n");
}
else
{ //check algorithm
//if algorithm is false print invalid, otherwise the other lines of code become useful

while (x != 0)
{
x = number / 10 ;
int  z = number % 10;
int  y = x % 10; //number in second last position;
number = number/10;
if (y < 9)
{
total_sum = total_sum + 2*y + z;
}
else
{
total_sum = total_sum + (y % 10) + (y /10) + z;
}
}

if (total_sum % 10 != 0)
{
printf("INVALID\n");
}

//here, need to check luhn's algorithm. if false print invalid. if true, continue onwards.
else if (number / 10000000000000 == 34  || number / 100000000000 == 34)
{
printf("AMEX\n");
}
else if (number / 100000000000000 == 4  || number / 1000000000 == 4)
{
printf("VISA\n");
}
else if (number / 100000000000000 == 51  || number / 1000000000000 == 52 || number / 1000000000000 == 53 || number / 1000000000000 == 54 || number / 1000000000000 == 55 )
{
printf("MASTERCARD\n");
}
else
{
printf("INVALID\n");
}
}
}
``````
• We'd be happy to, but you need to add some context. What are you having problems with? Please edit the question and tell us what you're having problems with, what you've tried, etc. This is what is called a naked code drop - no real explanation of the problem, and is against forum rules., so please add some context. ;-) Sep 27, 2020 at 22:12
• Hi, I'm really sorry, I didn't realise this! I have now edited. Sep 28, 2020 at 7:33
• i would suggest that you use string instead of numbers. you can convert it to an array of integers later by using: int = char[i]-'0'; as majority of your errors come from calculating the individual digits. Sep 28, 2020 at 8:27

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

int parseint(char cnum); int main(void) { //variable declaration string cardnoS = get_string("enter your card no:\n"); int n=strlen(cardnoS); int cardnoI[n],sum2=0,sum1=0;

``````for(int i=0;i<n;i++)
{   //cinverting char to int
cardnoI[i]=parseint(cardnoS[i]);

}
for(int i=n-2;i>=0;i-=2)
{    //calculating sum of alternate digits
sum1+=cardnoI[i+1];
if(2*cardnoI[i]>=10)
{

sum2+=((2*cardnoI[i]/10)+(2*cardnoI[i]%10));
}else
{
sum2+=2*cardnoI[i];
}

}

int sum=sum1+sum2;

if(sum%10==0)
{
printf("synta is valid");
}else
{
printf("the syntax is not valid ");
}
``````

}

int parseint(char cnum) { int num = cnum-'0'; return num; }

refer to this

Thanks everyone!

Have now fixed the issue :)