I just started CS50 this week with no background in CS. I don't understand what is wrong with my solution to credit, in pset1. I keep trying the example card number '378282246310005', but it returns invalid. It is very weird because when I check the length, sum, or id arguments alone, the output is in fact "AMEX"... i.e. by themselves each of the arguments work, just not together. Again, I am new to CS and I love it so far! I feel like I might be implementing operators wrong? Any help would be greatly appreciated. Side note: first post here and I'm sorry if something like this has been asked but I was unable to find a problem like mine. I think it is something minor?
#include <cs50.h>
#include <stdio.h>
long long card;
int checksum(long long h);
int checklength(long long k);
int checkid (long long q);
int main (void)
{
printf("Credit Card Number Please: \n");
card= get_long_long();
int x= checksum(card)%10;
int y= checklength(card);
int z= checkid(card);
if((y==16) && (z>=51 && z<=55) && (x==0) )
{
printf("MASTERCARD\n");
}
else if((y==15) && (z==34 || z==37) && (x==0))
{
printf("AMEX\n");
}
else if((y==13||y==16) && (z%10==4) && (x==0))
{
printf("VISA\n");
}
else printf("INVALID\n");
}
int checksum(long long h)
{
h= card;
int total=0;
for (int i=0; i<10;i++)
{
while (card)
{
int j= card%10;
int k= (card/10)%10;
if (2 * k< 10)
{
total= total + k* 2;
}
else
{
total = total + 1 + (2 * k - 10);
}
total= total+ j;
card/=100;
}
}
return total;
}
int checklength(long long k)
{
int counter=0;
while (k != 0)
{
k/=10;
counter++;
}
return counter;
}
int checkid (long long q)
{
while(q>100)
{
q/=10;
}
return q;
}