Update # attempt 2:
I am not getting valid output, code complies fine.
What am I missing?
code follows:
including cs.50 and stdio ( i don't think i need math )
bool checksum( long long cc); // for Lhun's == 0 valid != 0 invalid.
int main(void){
long long cc, i; //i is just an int to play with
int count, firstdigit, firsttwodigit, number, i, n; //first form THE END! *using i*
printf("number: ");
cc = get_long_long(); // it was suggested that i use an unsigned long long, but it seems the cs50
// library doesn't contain a get_unsignedlong_long function.
for (count = 1, i = number; i >= 100; count++) // count moves us through the card's digits
{
i = 1/10; // getting the last number
}
firsttwodigit = i;
firstdigit = i/10 // dividing by 10 to get first digit from the END
++count; // the above line makes i count backwards and leaves first two counting forward.
// this section splits the cards into their brandnames
if ((count == 13 || count == 16) && first_dig == 4 && checksum(cc) == true)
{
printf ("VISA\n");
else if ((count == 15) && (first_two == 34 || first_two == 37) && checksum(cc) == true )
printf ("AMEX\n");
else if((count == 16) && (first_two == 51 || first_two == 52 || first_two == 53 ||
first_two == 54 || first_two == 55 ) && checksum(cc) == true)
printf ("MASTERCARD\n");
else printf ("INVALID\n");
}
bool checksum( long long cc)
{
long value = number;
int i, n, digits[20], sum = 0
for (i = 0; value > 0; ++i)
{
digits[i] = value % 10;
value /= 10;
}
for (n = 0; n < i; n++)
{
if ((n % 2) == 0)
{
sum += digits[n];
}
else sum += (figits[n] * 2);
}
if sum (% 10 == 0)
{
return true;
}
else return false;
many thanks to ChrisG for the modulo & division advice. many thanks to bobbyp for teaching me about booleans for the checksum.