0

So, this is probably not a good solution of the problem, but I hoped it would at least work, after struggling with it for quite some time. It made me happy to see it worked for the first number I tested (AMEX), however, when I ran the cs50 test, it turned out it works only for certain cases, and I cannot figure out where the problem is.

Also, when I run the program and enter the credit card number, in certain cases it accepts it and displays result as either invalid or the card name, but in other cases it just keeps prompting me for new entry, and I don't see the reason.

I would really appreciate any help...

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

int main(void)
{
   //Prompt the user for credit card number

  long long ccnumber = get_long_long ("Number: ");

  long long sum1=0;
  long long sum2=0;
  long long di=0;
  long long dj=0;
  long long d1=ccnumber%10;
  long long d2=0;


// Sum the digits that won't be multiplied by 2 (every other digit starting from the last one)

  long long number1=ccnumber;
  while (number1>0)
  {
    number1=number1/100;
    di=number1%10;
    sum1+=di;
 }

// Sum every other digit multiplied by 2, starting with the number’s second-to-last digit

long long number2=ccnumber/10;
long long d2temp=(number2%10);

if(d2temp*2>9)
{
    d2=d2temp%10 + (d2temp/10)%10;
}
else
{
    d2=d2temp;
}

while(number2>0)
{
    number2=number2/100;
    di=number2%10;

    if(di*2>9)
{
    dj=(di*2)%10 + (di*2/10)%10;
}
else
{
    dj=di*2;
}

    sum2+=dj;
}


   long long totalsum=sum1+sum2+d1+d2;



 // Check if the card is valid and what type it is if so

if (totalsum%10==0)
{
    if( (ccnumber>=4000000000000 && ccnumber<5000000000000) || (ccnumber>=4000000000000000 && ccnumber<5000000000000000))
    {
        printf("VISA\n");
    }
    else if( (ccnumber>=340000000000000 && ccnumber<350000000000000) || (ccnumber>=370000000000000 && ccnumber<380000000000000))
    {
        printf("AMEX\n");
    }
    else if(ccnumber>=5100000000000000 && ccnumber<5600000000000000)
    {
        printf("MASTERCARD\n");
    }
    else
    {
        printf("INVALID\n");
    }

}

else
{
    printf("INVALID\n");
}




}
2
  • Could you please provide some example of cards not recognised correctly?
    – Blauelf
    Dec 6, 2018 at 16:55
  • Most of the valid numbers are recognized as invalid, for example 371449635398431 which corresponds to AMEX, it is evident that the algorithm is not correct, apart from the mentioned problem. Maybe you want to review the entire program, but I think that this task corresponds to the user. For my part I am willing to answer specific questions, but completely debug a program ....
    – MARS
    Dec 6, 2018 at 17:53

1 Answer 1

0

The only reason for the behavior of the program that you describe is the function get_long_long, as it is implemented will only accept integers numbers, without any type of character. Are you entering the numbers manually, or maybe you use copy and paste? . If you use copy and paste it is more than possible that you are copying some non-numeric character, and therefore the function get_long_long() is rejecting the entry.

2
  • this has helped me a lot...I was actually copying the numbers from the cs50 test, and indeed I would copy the blank space at the end of the number every time, but I probably wouldn't notice it myself any time soon....this made it possible for me to correctly test the program for many inputs with printing sum1 and sum 2 at every step, as well as d2...then I realized that I didn't multiply d2temp by 2 when calculating d2, and that was the only mistake...after correcting that, the program worked correctly, so I will upload the new correct code...thanks a lot!
    – al12e13
    Dec 6, 2018 at 21:13
  • I'm glad to hear that, good job. Onward!
    – MARS
    Dec 6, 2018 at 22:20

You must log in to answer this question.

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