0

the following code for CS50's hacker edition Bad Credit is compiling but is not executing. Would really appreciate a review:

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

// prototype

int len(long long x); 
int firstdigits(long long y);
bool valid(long long p, int i);

int main(void) // main function where the code runs determining whether all the conditions fit
{
    long long number;

    do
    {
        printf ("Enter your credit card number: \n");
        number = GetLongLong();
    }
    while (number <= 0);

    if (len(number) == 13 && 50 > firstdigits(number) && firstdigits(number) > 39 && valid(number, len(number)) == true)
    {
        printf("VISA\n");
    }
    else if (len(number) == 15 && 37 >= firstdigits(number) && firstdigits(number) >= 34 && valid(number, len(number)) == true)
    {
        printf("AMEX\n");
    }
    else if (len(number) == 16 && 50 > firstdigits(number) && firstdigits(number) > 39 && valid(number, len(number)) == true)
    {
        printf("VISA\n");
    }
    else if (len(number) == 16 && 55 >= firstdigits(number) && firstdigits(number) >= 51 && valid(number, len(number)) == true)
    {
        printf("MASTERCARD\n");
    }
    else
    {
        printf("INVALID\n");
    }
}

int len(long long x) // defining a function that returns the length of the credit card number
{
    int counter = 0;
    do
    {
        x = x / 10;
        counter ++;
    }
    while (x != 0);

    return counter;
}

int firstdigits(long long y) // defining a function that returns the first two digits of the number to determine the company
{
    long long z;
    do
    {
        z = y / 10;
    }
    while (y >= 100);

    return z;
}

bool valid(long long p, int i) // defining a function that determines whether the credit card is valid or not
{
    int sum1 = 0, sum2 = 0, final = 0, m[i]; // sum1 = sum of all the digits that are to be multiplied by 2, and sum2 is otherwise

    for (i = 0; i > 0; i++)
    {
        m[i] = (p % 10); // seperating the digits

        if (len(p) == 13)
        {
            sum2 = m[0] + m[2] + m[4] + m[6] + m[8] + m[10] + m[12];
        }
        else if (len(p) == 15)
        {
            sum2 = m[0] + m[2] + m[4] + m[6] + m[8] + m[10] + m[12] + m[14];
        }
        else if (len(p) == 16)
        {
            sum2 = m[0] + m[2] + m[4] + m[6] + m[8] + m[10] + m[12] + m[14] + m[16];
        }
        else
        {
            printf("INVALID");
        }

        if ((m[i] * 2) > 9) // for seperating the digits that are multiplied by 2
        { 
            m[i] = (m[i] / 10) % 10; 
        }
        else
        {
            m[i] = m[i] % 10;
        }

        if (len(p) == 13)
        {
            sum1 = m[1] + m[3] + m[5] + m[7] + m[9] + m[11] + m[13];
        }
        else if (len(p) == 15 || len(p) == 16)
        {
            sum1 = m[1] + m[3] + m[5] + m[7] + m[9] + m[11] + m[13] + m[15];
        }
        else
        {
            printf("INVALID");
        }

        final = sum1 + sum2; 
    }
    return final % 10;
}
4
  • What happens when you step through the program using gdb? Sep 19 '16 at 10:03
  • I haven't used gdb for debugging. But an online compiler displays: stdin copy Standard input is empty compilation info prog.c:2:17: fatal error: cs50.h: No such file or directory compilation terminated. stdout Standard output is empty Sep 19 '16 at 12:56
  • So, it is not compiling? My guess is that it is not finding the cs50 header file but I don't know why that would be. Why aren't you using the course IDE? Sep 19 '16 at 17:42
  • When I check using the link provided with course - :) credit.c exists :) credit.c compiles :( identifies 378282246310005 as AMEX \ killed by server :( identifies 371449635398431 as AMEX \ killed by server :( identifies 5555555555554444 as MASTERCARD \ killed by server :( identifies 5105105105105100 as MASTERCARD \ killed by server :( identifies 4111111111111111 as VISA \ killed by server :( identifies 4012888888881881 as VISA \ killed by server :) identifies 1234567890 as INVALID :) rejects a non-numeric input of "foo" :) rejects a non-numeric input of "" Sep 20 '16 at 4:54

You must log in to answer this question.

Browse other questions tagged .