0

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");
        }
        }
}
3
  • 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. ;-)
    – Cliff B
    Sep 27, 2020 at 22:12
  • Hi, I'm really sorry, I didn't realise this! I have now edited.
    – AbbiePBC
    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

2 Answers 2

1

#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

0

Thanks everyone!

Have now fixed the issue :)

2
  • Please don't add "thank you" as an answer. Instead, accept the answer that you found most helpful. - From Review
    – Cliff B
    Oct 7, 2020 at 8:35
  • I ended up fixing it myself, without these inputs. I was thanking them for their time and effort, but it didn't actually help me.
    – AbbiePBC
    Oct 9, 2020 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .