0

I'd like to preface by saying I am pretty much a total newbie, I am just giving the hacker pset a shot because I want a fun challenge. I feel pretty good about my code overall and it compiles, however I get a segmentation fault no matter what value I input. I am not sure which section is causing the issue so I'll just post the full code here, would really appreciate anyone who could identify my mistake(s):

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int algorithm(string a);
bool isValid(string b);

int main(void)
{
    long long num = 0;
    do
    {
        printf("Please enter your credit card number: ");
        num = GetLongLong();
    }
    while (num < 0);

    char s[20];
    sprintf(s, "%lld", num);

    if (isValid(s))
    {
        if (strlen(s) == 15)
        {
            printf("Amex\n");
        }
        else if ((strlen(s) == 13 || strlen(s) == 16) && (s[0] == 5))
        {
            printf("Mastercard\n");
        }
        else if (strlen(s) == 16 && s[0] == 4)
        {
            printf("Visa\n");
        }
    }
    else
        printf("INVALID\n");
}

int algorithm(string a)
{
    string prod = NULL;
    string dig = NULL;
    int sum = 0;

    for (int i = strlen(a) - 2, j = 0; i > 0; i-=2, j++)
    {
        prod[j] = a[i] * 2;
    }
    for (int k = 0; k < strlen(prod) - 1; k++)
    {
        if (prod[k] >= 10)
            dig[k] = prod[k] % 10 + 1;
        else
            dig[k] = prod[k];
    }
    for (int l = 0; l < strlen(dig) - 1; l++)
    {
        sum += dig[l];
    }
    return sum;
}

bool isValid(string b)
{
    int sum2 = 0;
    for (int h = strlen(b) - 1; h > 0; h -= 2)
    {
        sum2 += b[h];
    }

    int sum = algorithm(b);
    int total = sum + sum2;
    if (total % 10 == 0)
        return true;
    else
        return false;
}
1

I'd suggest a different method of turning your long long into an array:

(1) Declare a char array with char s[20]; (20 digits should be enough for a long long),

(2) Use sprintf to pass num into char array s.

Otherwise, you've got your function names the wrong way round, and you've forgotten to have one call the other.

Oh and your code is not technically correct for all cases, as you're not checking the second digit for Mastercard (s[1] > 0 and <=5) or any digits for Amex. The problem set tells you the exact digits that the cards start with, so to be 100% correct you should have all that in your code.

1
  • gotcha, thanks for the thorough answer. So I've tried to fix it with your instruction and I've edited the code in the OP accordingly (minus the checking of the exact digits because that's fairly straight-forward). isValid is now calling algorithm. However I am still getting a seg fault, and it seems to be happening right around the sprintf function. Thoughts on why this is happening? Jun 8 '16 at 18:51
0

The problem seems quite clear, Notwithstanding the terrible format

string s = (string) num;

chains are peculiar in c, it really is a low-fat char * s, but you can not turn without plus a number to a string

2
  • yeah that's definitely where the main issue is. Trying to fix it now with sprintf but still getting a seg fault. Any tips on how to improve the formatting? Thanks Jun 8 '16 at 19:09
  • Dear friend, you asked a concrete answer and that's what I gave you. I do not want to be interpreted as a critique other users but those answers are not ordered spoil the spirit of the edition hacker, if you look is the standard edition, you will see that many clues on how to solve the problem are given, which does not happen with the hacker edition, of course this is intentional, draw your own conclusions. The format is fine, I think it was a problem with the page.
    – MARS
    Jun 8 '16 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .