pset1: credit - code not working

Hello looking for help again.

Ok some update. I managed to put my codes together, but something is still wrong because every number I checked returned invalid. I'd like to know if my "checker" code was ok or did I miss something?

int a = 0;
int sum = 0;
for (int i = n - 2; i >= 0; i = i - 2)
{
int digit = cn[i] - 48;
int times2 = 2 * digit;
sum += times2 / 10 + times2 % 10;
}
int sum2 = 0;
for (int i = n - 1; i >= 0; i = i - 2)
{
int digit = cn[i] - 48;
sum2 += digit;
}
int sum3 = sum + sum2;
if (sum3 / 10 == 0)
{
a = 0;
}
else
{
a = 1;
}

At first I wanted to use bool type for this checker, but error message popped up when I tried to compile. Don't know why. So instead of bool type, I assigned an integer at the beginning and did the following instead. Is this practical? and why didn't bool type work?

if (a == 1)
{
printf("INVALID\n");
}

Please give me some pointers. Thank you!

• Why are you getting the number as a string? The specification says you can use get_long_long to get the number as a long long. Besides that, getting the digits with / and % is a good idea. Try writing out on a piece of paper a long number, and use these two operators to get the digits (starting from the ones place). You can make a loop from that. Aug 25 '17 at 0:07
• It seems getting the number as a string can work too? I'd like to try using get_long_long but I'd like to make this work first since I already started. Thanks for letting me know the / and % part was a good idea! I just updated the code I have now but it's not giving me the correct output. Aug 25 '17 at 4:58
• You can do it either way, it's just easier operating on an integer, to use / and % you'll have to turn your string into an integer, so it saves a step. Aug 25 '17 at 5:02
• the bool data type is in <cs50.h>, did you include it? Aug 25 '17 at 5:05
• Now I see what you're saying... yeah that extra step took me a long time! I included <cs50.h> though. Aug 25 '17 at 6:21

Your final comparison is not doing what you think.

if (sum3 / 10 == 0)
{
a = 0;
}

If the sum is 60, the card should be valid because it ends in a 0, but your comparison will try 60/10 which is 6 not 0, so the if statement will not trigger.

If you want to see if the last digit of a number is 0, that is the same as checking if the number is divisible by 10. You want the -remainder- of 60/10 to be 0. You need the modulus operator.

This leads to one of the most important concepts of debugging a program. Don't stare at the code looking for a mistake, add printf statements or run a debugger to check intermediate values.

For instance, if all of your card numbers are showing up invalid, you might wonder why the if statement above is never true. To investigate, add this above the if statement:

printf("sum3=%d sum3/10=%d\n", sum3, sum3/10);
if (sum3 / 10 == 0)
{
a = 0;
}

If you're not getting what you expect here, you can jump further up the code and add printf statements until you find what's not working as you expect.