first time posting. I swear I looked for answers in other segmentation fault questions already, but I am new to programming and to troubleshooting/researching effectively.
My PSet1 program "Credit" compiles and it properly handles every single check in check50... except for two similar cases. I get "Segmentation fault" when I input the credit card # 4222222222222 and 4222222222223. Why does this happen?
Full code is below. I imagine my code is not efficient and the notes are more for my benefit than yours, but it's what I got so far:
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
long cc_number = get_long("Number: ");
int cc_length = floor(log10(labs(cc_number)));
long number = cc_number;
int arr[cc_length];
//write the credit card number as an array starting from the end using remainders
for (int i = cc_length; i >= 0; i--)
{
arr[i] = number % 10;
number = number / 10;
}
//perform Luhn's algorithm
int sum = 0;
//starting with 2nd-to-last digit in array, double each value
for (int i = cc_length - 1; i >= 0;)
{
int checksum_add = arr[i] * 2;
//if doubled value is 10 or greater, add it's digits together (which is the same as subtracting 9)
if (checksum_add > 9)
{
sum = sum + checksum_add - 9;
}
else
//if it is not 10 or greater, just add the value
{
sum = sum + checksum_add;
}
//decrement to get every other digit in array
i = i - 2;
}
// now add up all remaining digits' values
for (int i = cc_length; i >= 0;)
{
sum = sum + arr[i];
i = i - 2;
}
//perform checks for various credit cards based on length, first digit(s), and Luhn's algorithm result
if (cc_length == 14 && arr[0] == 3 && (arr[1] == 4 || arr[1] == 7) && sum % 10 == 0)
{
printf("AMEX\n");
}
else if (cc_length == 15 && arr[0] == 5 && arr[1] > 0 && arr[1] < 6 && sum % 10 == 0)
{
printf("MASTERCARD\n");
}
else if ((cc_length == 12 || cc_length ==15) && arr[0] == 4 && sum % 10 == 0)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
Results for cs50/problems/2021/x/credit generated by check50 v3.3.0
:) credit.c exists
:) credit.c compiles
:) identifies 378282246310005 as AMEX
:) identifies 371449635398431 as AMEX
:) identifies 5555555555554444 as MASTERCARD
:) identifies 5105105105105100 as MASTERCARD
:) identifies 4111111111111111 as VISA
:) identifies 4012888888881881 as VISA
:( identifies 4222222222222 as VISA
expected "VISA\n", not ""
:) identifies 1234567890 as INVALID
:) identifies 369421438430814 as INVALID
:) identifies 4062901840 as INVALID
:) identifies 5673598276138003 as INVALID
:) identifies 4111111111111113 as INVALID
:( identifies 4222222222223 as INVALID
expected "INVALID\n", not ""
To see the results in your browser go to https://submit.cs50.io/check50/82b01a7344c69be463d9660c64e599d081445fbe