ccs50credits

Why do i keep getting an invalid feedback?


I've been working on the cs50 pset1 credit for the past 36 hours. My code is not generating any errors but when i enter my card details it shows invalid, what could be the problem. I'm a noob, guys please help out.

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

 int main (void)
{
    long long ccnumber;

     do 
     {
         //Prompting the user for his creditcard number
         ccnumber=get_long_long("What is your creditcard number\n");
     }
     while (ccnumber<= 0);

     //validating the credit card number to ascertain if its upto 13. 14 or 16
    long long numbers = ccnumber;
     int totaldigits = 0; 

     while (numbers > 0)
     {
         numbers = numbers/10;
         totaldigits++;
     }
     if(totaldigits != 13 || totaldigits !=14 || totaldigits !=16)
     {
         printf("Invalid\n");
     }

     //Starting from the second to last digit
     //And multiplying through by 2
     int number1 = ((ccnumber/10) % 10) *2;
     int number2 = ((ccnumber/1000) % 10) *2;
     int number3 = ((ccnumber/100000) % 10) *2;
     int number4 = ((ccnumber/10000000) % 10) *2;
     int number5 = ((ccnumber/1000000000) % 10) *2;
     int number6 = ((ccnumber/100000000000) % 10) *2;
     int number7 = ((ccnumber/10000000000000) % 10) *2;
     int number8 = ((ccnumber/10000000000000000) % 10) *2;

     //Getting the Sum of the first number series
     int sum1;
         sum1 = (number1 + number2 + number3 + number4 + number5 + number6 + number7 + number8);

     //Populating the second number series
     int number9 = ccnumber % 10;
     int number10 = ((ccnumber/100) % 10);
     int number11 = ((ccnumber/10000) % 10);
     int number12 = ((ccnumber/1000000) % 10);
     int number13 = ((ccnumber/100000000) % 10);
     int number14 = ((ccnumber/10000000000) % 10);
     int number15 = ((ccnumber/1000000000000) % 10);
     int number16 = ((ccnumber/100000000000000) % 10);

    int sum2;
        sum2 =(number9 + number10 + number11 + number12 + number13 + number14 + number15 + number16);

     //Adding sum1 and sum2
     int checksum;
         checksum = sum1 + sum2;
     if (checksum % 10 != 0)
     {
         printf("invalid/n");
      }

     //Verifying for visacard(1)
     if(totaldigits == 13)
     {
         printf("Visa\n");
     }

     // Verifying for visa(2)
     if(totaldigits == 16)
     {
         long long visa;
         visa = ccnumber/1000000000000000;
       if(visa!=4)
       {
           printf("Invalid\n");
       } 
         else 
         {
             printf("VISA\n");
         }
         //Verifying for MasterCard
         long long master;
         master = ccnumber/100000000000000;
         if(master!=51 || master!=52 ||master!=53 ||master!=54 ||master!=55)
         {
             printf("Invalid\n");
         }
         else
         {
             printf("MASTER\n");
         }
     }

       //Verifying for Amex
     }

/counter What is your creditcard number 2221000000000009 Invalid invalid/nInvalid Invalid


Solution

  • First of all, some of your if conditions are wrong. For example,

    if(totaldigits != 13 || totaldigits != 14 || totaldigits != 16)
    

    You want to check if the totaldigits is 13 or 14 or 16, but what you check here is if totaldigits is not 13, then print invalid or if totaldigits is not 14 then, print invalid and so on. Hence, it will always print invalid. You need to use && (AND) instead of || (OR).

    The same problem exists in this line as well.

    if(master!=51 || master!=52 ||master!=53 ||master!=54 ||master!=55)
    

    Another problem is that you put 1 unnecessary 0 to division in this line, so it always yields 0.

    int number8 = ((ccnumber/10000000000000000) % 10) *2; // 1 zero unnecessary
    

    As it is suggested in the comments, you may need to consider creating an array for storing individual digits instead of using 16 different integers. When you have an array you can do the divisions in a for loop so that you don't get confused with a lot of zeros. As an example, you can refer to this link A solution for cs50's credit card validation problem .