1

I have this C code for Luhn’s Algorithm. it is running fine by testing some credit card numbers, but the code checking gives me these errors. Why?

when running the code, all credit card numbers are checked correctly, but the code checking say this : expected "xxxx", not "" as shown below.

The file name is credittest.c

The code is below.

the error check gives these errors below:

~/pset1/credit/ $ check50 cs50/problems/2020/x/credit
Connecting.....
Authenticating....
Verifying.....
Preparing.....
Uploading............
Waiting for results...........................
Results for cs50/problems/2020/x/credit generated by check50 v3.1.2
:) credit.c exists
:) credit.c compiles
:( identifies 378282246310005 as AMEX
    expected "AMEX\n", not ""
:( identifies 371449635398431 as AMEX
    expected "AMEX\n", not ""
:( identifies 5555555555554444 as MASTERCARD
    expected "MASTERCARD\n", not ""
:( identifies 5105105105105100 as MASTERCARD
    expected "MASTERCARD\n", not ""
:( identifies 4111111111111111 as VISA
    expected "VISA\n", not ""
:( identifies 4012888888881881 as VISA
    expected "VISA\n", not ""
:( identifies 1234567890 as INVALID
    expected "INVALID\n", not ""
:( identifies 369421438430814 as INVALID
    expected "INVALID\n", not ""
:( identifies 4062901840 as INVALID
    expected "INVALID\n", not ""
:( identifies 5673598276138003 as INVALID
    expected "INVALID\n", not ""
:( identifies 4111111111111113 as INVALID
    expected "INVALID\n", not ""

below is the code:


//prompts the user for a credit card number and then reports (via printf) whether
//it is a validity American Express, MASTERCARD, or Visa card number

//All American Express numbers 15-digit start with 34 or 37;
//most MASTERCARD numbers 16-digit start with 51, 52, 53, 54, or 55
///all Visa numbers 13- and 16-digit start with 4


#include<stdio.h>
#include<cs50.h>
#include<math.h>
bool validity(long cardnumber);
int find_length(long cardnumber);
bool checksum(long cardnumber);
void printcardbrand(long cardnumber);



int main(void)
{
    long cardnumber;

    do
    {
        cardnumber = get_long("What is the card number: ");
    }
    while (cardnumber < 0);



    if (validity(cardnumber))
    {
        printcardbrand(cardnumber);
    }

    else
    {
        printf("INVALID\\n\n");
    }

}



bool validity(long cardnumber)
{
    int len = find_length(cardnumber);

    if((len == 13 || len == 16) && checksum(cardnumber) && ((cardnumber >= 4e12 && cardnumber < 5e12)|| (cardnumber >= 4e15 && cardnumber < 5e15)))
    return true;

    if(len == 16 && checksum(cardnumber) && (cardnumber >= 51e14 && cardnumber < 56e14))
    return true;

    if(len == 15 && checksum(cardnumber) && ( (cardnumber >= 34e13 && cardnumber < 35e13) || (cardnumber >= 37e13 && cardnumber < 38e13) ) )
    return true;

    else
    return false;

}




int find_length(long cardnumber)
{
    int len;

    for (len = 0; cardnumber != 0; cardnumber /= 10)
    {
        len++;
    }
    return len;
}



bool checksum(long cardnumber)
{
    int sum = 0;

    for (int i = 0; cardnumber != 0; i++, cardnumber /=10)
    {
        if (i % 2 == 0)
        {
            sum = sum + cardnumber % 10;  //get remainder, i.e. the even index digit
        }

        else
        {
            int digit = 2 * (cardnumber % 10);
            sum = sum + digit / 10 + digit % 10;
        }
    }

    return (sum % 10) == 0;

}





void printcardbrand(long cardnumber)
{
    if ( (cardnumber >= 34e13 && cardnumber < 35e13) || (cardnumber >= 37e13 && cardnumber < 38e13) )
    {
        printf("AMEX\\n\n");
    }

    else if ( (cardnumber >= 51e14 && cardnumber < 56e14) )
    {
        printf("MASTERCARD\\n\n");
    }

    else if ( (cardnumber >= 4e12 && cardnumber < 5e12) || (cardnumber >= 4e15 && cardnumber < 5e15) )
    {
        printf("VISA\\n\n");
    }


}



// checksum, card length, starting digits will decide which card it is.

//last line of output be AMEX\n or MASTERCARD\n or VISA\n or INVALID\n


``
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
xin pds
  • 73
  • 6
  • What compiler is being used? How many bits in your `long` type? On my system, a long can only hold 10 digits (2147483647, not even ten 9's). Most credit card numbers are longer than that. – jwdonahue Jun 21 '20 at 03:55
  • Every `printf` format string should end with `\n` not `\\n\n`. – user3386109 Jun 21 '20 at 04:00
  • The test software is not getting any output. Is printf how you are supposed to put out results? It might help if you posted something others could compile. Not everyone has `cs50.h`. – Dennis Sparrow Jun 21 '20 at 04:02
  • I changed `long` to `int64_t` and fixed the `printf` statements and this works: https://ideone.com/sTFUnL It might be, as @jwdonahue mentioned, that `long` is not 64 bits. – Retired Ninja Jun 21 '20 at 04:06
  • thanks, it asked to print out at end with \n and a new line, that is why \\n\n there. – xin pds Jun 21 '20 at 04:21
  • i see people use: long long variable to define a variable, will this do the trick, i will try. – xin pds Jun 21 '20 at 04:22
  • my code uploaded above works when check every credit card numbers. just the code checking gives the errors. – xin pds Jun 21 '20 at 04:25
  • cs50.stackexchange.com – M.M Jun 25 '20 at 04:37

1 Answers1

0

Your printf calls say "AMEX\\n\n" when it should just be "AMEX\n"

The check50 wording is not great and technically you did what they said literally but it isn't what they meant. Check50 says expecting "AMEX\n" but what the output in the terminal should be AMEX all by itself on one line. To do that, you need to use the below code.

printf("AMEX\n")

and obviously the same for all other credit cards

rfii
  • 562
  • 3
  • 14
  • yes, thanks. The file name is credittest.c. The code checking will only check the file credit.c; that is why error comes up because there is nothing in credit.c; once I saved this file as credit.c and all good. – xin pds Jun 22 '20 at 10:56