# Pset 1 Credit Card ( help needed )

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.

• id love to learn how to write the code for the checksum i found it really tough, that is where i am stuck now Commented Apr 28, 2020 at 4:15

Good job reaching that point and giving a shot to the hacker pset too! Don't be dismayed if you don't succeed with the first try, keep trying!

First of all when you declare a variable, you don't have to put it in parentheses like you have:

long long(cc);


Something like that is more readable and used mostly:

long long cc;


Also since there aren't any signed credit card numbers (that is negative card numbers) you can also specify it as unsigned.

unsigned long long cc;


Now as to how to get each number, there is a simple arithmetic trick using modulo and integer division. Say we have the following code:

unsigned long long cc = 123;
unsigned long long last = c % 10;
unsigned long long remaining = c / 10;


After the execution of the code, last will be equal to the last digit in cc, because if you divide 123 by 10 you will have a remainder 3 (try it in paper if you don't believe me).

Also remaining will be equal to the remaining digits of cc, excluding the last, because if you divide 123 by 10 you will have a quotient 12.

Now if instead of a new variable remaining you use cc, and use a while loop to loop until cc == 0, you can get each digit, from last to first.

Example code:

#include <stdio.h>
#include <math.h>   // don't forget to link the math library! (-lm)

int main(void) {
unsigned long long num = 1472583690;

// a mathematical way to get the number of digits of an integer value
printf("Number of digits: %d\n", (int) (log10(num) + 1));

while (num != 0) {
unsigned int last = num % 10;
printf("%u\n", last);

num = num / 10;
}

return 0;
}


Example Output:

Number of digits: 10
0
9
6
3
8
5
2
7
4
1

• Ok, so I've tried the above ( i was getting errors using get_long_long and trying to use it as an unsigned so i went back to using one that could potentially accept negative numbers..) Commented Jan 24, 2017 at 2:42
• I think using while n > 0 is better instead of while n != 0 just so that negatives dont come into play :) Commented Aug 17, 2018 at 17:25