1

iam a beginner to programming and recently started cs50,in week1 there is a credit problem which stats: Implement a program that determines whether a provided credit card number is valid according to Luhn’s algorithm. though the program is getting compiled and is executed the output is not what i desired,it would be really helpful if anyone can point out the mistakes.

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

    int main(void)
    {
        int count=0,mod[20],sum1=0,sum2=0,sum=0;
        long c,h;
        printf("ENTER THE CREDIT CARD NO");
    c=get_long_long();
h=c;
     while(h!=0)
        {
            h=h/10;
            count=count+1;

        }


        for(int i=0;i<count;i++)
        {
            mod[i]=c%10;
        c=c/10;
        }

        for(int i=1;i<count;i+=2)
        {
        mod[i]=(mod[i]*2);
            sum1=sum1+(mod[i]/10)+(mod[i]%10);
             sum2=sum2+mod[i-1];
        }
        sum=sum1+sum2;
        int a=mod[count-1];
        int b=mod[count-2];



        if(count==15)
              {
                  if(a==3)
              {
               if(b==4||b==7)
              {
                  if((sum%10)==0)
    printf("your credit card is a valid american express card");
               else
               printf("your credit card is an invalid american express card");

        }
              }
              }
        if(count==13||count==16)
        {
        if(a==4)
        {
            if((sum%10)==0)
               printf("your credit card is a valid visa card");

               else
                      printf("your credit card is an invalid visa card");
    }
         }




         if(count==16)
        {
            if(a==5)
             {
                 if((b>=1)&&(b<=5))
             {
             if((sum%10)==0)
    printf("your credit card is a valid  mastercard");

               else
               printf("your credit card is an invalid master card");

             }
             }

    }

    }

![in the teminal after enter the credit card no,it does nothing ]1

0

Your digit counter "destroys" c, so there's nothing left for the next for loop. Create a copy to destroy instead. Or combine the two loops.

You shouldn't change the digits in mod, as you still need them in your if-else monster (which always should print exactly one word). Consider && to reduce the nesting levels to one or two. && has higher precedence than ||, so chances are you might have to add parentheses.

8
  • thanks,but still after the change you mentioned program is not working properly
    – ironman19_
    May 15 '18 at 12:57
  • In the code you posted, h never receives a value. And with this arbitrary (not controlled by you) value you start into the loop.
    – Blauelf
    May 15 '18 at 13:05
  • actually i did assigned the value to h but just forgot to mention here but still the program is just giving me nothing.
    – ironman19_
    May 15 '18 at 13:12
  • Are there code paths through the code that don't print anything? You need to print exactly one word (like "VISA" or "INVALID"). And your checksum code ignores the first digit if the number has an odd number of digits.
    – Blauelf
    May 15 '18 at 13:25
  • Oh, and maybe you should begin debugging. This could be eprintf statements (eprintf is defined in cs50 library and works like printf), or defining a breakpoint and using debug50 to step through your code.
    – Blauelf
    May 15 '18 at 13:29

You must log in to answer this question.

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