0

I just finished solving the credit problem and regularly used check50 to see my results, and then this happened. check50 sees that these numbers :

369421438430814

5673598276138003

are invalid, even though in every luhn calculator on the internet these numbers are valid ps:// I will leave links for the luhn calculator at the end of the post. ps2:// tried my code with every one of Paypal suggested credit cards and it works perfectly. ps3:// my code included at the end of the post just in case.

site1:https://goodcalculators.com/luhn-algorithm-calculator/?Num=4012888888881881 site2:https://www.dcode.fr/luhn-algorithm site3:https://www.mymathtables.com/numbers/luhn-algorithm-calculator.html

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

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>The 
Start Of The 
Program<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

int main(void)

{
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>||||||  Define 
variables Section  ||||||<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
long long credit_card_number;
long long credit_helper;
int number_yes;
int number_no;
int number_no_helper;
int number_y = 0;
int number_n = 0;
int number_n_h = 0;
int credit = 0;
int number_f;
long long digit_counter;

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>|||||| verfication of the user input section  ||||||<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  

do // using do/while to get only right values
{
    credit_card_number = get_long("enter your Number : "); 
}
while (credit_card_number < -1 || credit_card_number == 0);

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>||||||  try to know what type of credit card user have ||||||<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//  find the last 2 digits of the enterd number

credit_helper = credit_card_number; // ||||||  this step because i need (CCN) in another uses down  |||||| 
digit_counter = credit_card_number;

while (credit_helper > 100)
{
    credit_helper = credit_helper / 10;
}

// counting the number digits

while (digit_counter > 1)
{
    credit++;
    digit_counter = digit_counter / 10;
}


// get the number by digit order

while (credit_card_number > 1)
{
    // starts with number_yes >>>>>>>>>>>>>>>>>>>>>>>>>>||Every number must done with his counter||<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  
    number_yes = credit_card_number % 10; 

    credit_card_number = credit_card_number / 10;
    
    // numebr_yes loop for number_y counter
    
    while (number_yes > 0)
    {
        number_y++;
        number_yes--;
    }
    
    // starts with number_no ||number_no need to * 2||

    number_no = credit_card_number % 10;

    credit_card_number = credit_card_number / 10;
    
    number_no = number_no * 2;
    
    // statment to check if value have 2 digit inside of it so we can plus them *12 = 1 + 2*
    
    if (number_no >= 10)
    {
        number_no_helper = number_no;
        number_no = number_no % 10;
        number_no_helper = number_no_helper / 10;
        number_no = number_no + number_no_helper;
        
        // first loop for number_no and the counter for values with more than 1 digit
        
        while (number_no > 0)
        {
            number_n_h++;
            number_no--;
        }
        
    }
    
    // second loop for number_no and the counter for values with 1 digit only 
    
    while (number_no > 0)
    {
        number_n++;
        number_no--;
    }
}

// check if enterd number can be valid (last number should be 0)

number_f = (number_y + number_n + number_n_h) % 10;
//printf("%i\n", number_f);

if (number_f == 0 && credit > 12)
{
    
    //  compare the resault of credit_helper from while loop above
    // find what credit type based on last 2 numbers
    
    if (credit_helper <= 56 && credit_helper >= 51)
    {
        printf("MASTERCARD\n");
    }
    if (credit_helper == 37 || credit_helper == 34 || credit_helper == 36)
    {
        printf("AMEX\n");
    }    
    if (credit_helper < 50 && credit_helper > 39)
    {
        printf("VISA\n");
    }

}
else if (number_f >= 1 || credit < 13)
{
    printf("INVALID\n"); 
}  

}

2
  • 1
    founded from another man have the same problem aslo asked his question here. it's not a calculations problem it's a certificate problem with the start of credit number, master card & AMEX doesn't start with 56 or 36. Jul 15 at 16:02
  • You can answer your own question and mark it as solved so that it doesn't show as unsolved anymore. Aug 6 at 22:41
0

I think you entered the wrong values when determining whether the credit card is Amex, Visa, or MasterCard.

For Mastercard, you typed in (credit_helper <= 56 && credit_helper >= 51). Try switching the 56 to 55, since 56 isn't a correct number for MasterCard. The correct numbers are 51 52 53 54 55.

For Amex, you typed in (credit_helper == 37 || credit_helper == 34 || credit_helper == 36). However, the only correct values for Amex are 34 and 37, and not 36.

Hope I helped :)

You must log in to answer this question.

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