I have a credit card verification that is giving me a little bit of trouble. I can get the program to verify everything but Amex. I'm only needing to verify Discover, Visa, MC, and Amex. It simply tells me Amex cards are invalid. I've used a few different websites to gather my test numbers just to make sure my numbers weren't bad. Any help is appreciated.

using System;

public class creditcardnumbercheck
{
    static void Main(string[] args)
    {

        long number;

        Console.Write("Enter credit card number: ");
        number = Convert.ToInt64(Console.ReadLine());

        if (isValid(number))
        {
            Console.WriteLine("{0} is a valid credit card", number);
        }
        else
        {
            Console.WriteLine("{0} is not a valid credit card", number);
        }
    }// end Main


    /*return true if # is valid*/
    public static bool isValid(long number)
    {
        int n;
        long p;
        p = getPrefix(number, 1);

        if (p < 4)
        {
            return false;
        }

        if (p > 6)
        {
            if (getPrefix(number, 2) != 3)
            {
                return false;
            }
        }

        n = getSize(number);
        if (n < 13 || n > 16)
        {
            return false;
        }
        if ((sumOfDoubleEvenPlace(number) + sumOfoddPlace(number)) % 10 != 0)
        {
            return false;
        }
        return true;
    }

    /*Get result from step 2 in pdf*/
    public static int sumOfDoubleEvenPlace(long number)
    {
        int i, sum = 0;
        for (i = 1; i <= 16; i++)
        {
            if (i % 2 == 0)
            {
                sum = sum + getDigit((int)(number % 10) * 2);
            }
            number /= 10;
        }
        return sum;
    }

    /*return this # if it is a single digit, otherwise, return
    * the sum of two digits */
    public static int getDigit(int number)
    {
        if (number < 10)
        {
            return number;
        }
        else
        {
            return (number / 10) + (number % 10);
        }

    }

    /* return sum of odd place digits in number */
    public static int sumOfoddPlace(long number)
    {
        
        int i, sum = 0;

        for (i = 1; i <= 16; i++)
        {
            if (i % 2 == 1)
            {
                sum = sum + (int)(number % 10);
            }
            number /= 10;
        }
        return sum;
    }

  /* return true if the digit 'digit' is a prefix for number*/ 
    public static bool prefixMatched(long number, int digit)
    {
        if (getPrefix(number, 1) == digit || getPrefix(number, 2) == digit)
        {
            return true;
        }
        return false;
    }

    // return the number of digits in d
    public static int getSize(long digit)
    {
        int n = 0;
        while (digit != 0)
        {
            n++;
            digit /= 10;
        }
        return n;
    }

    /* return the first k number of digits from number. If the number of digits in   number is less than k, return number. */
    public static long getPrefix(long number, int k)
    {
        if (k == 1)
        {
            return number / 1000000000000000;
        }
        if (k == 2)
        {
            return number / 100000000000000;
        }
        return 0;
    }
}

How long are the credit card numbers you are using? In your getPrefix method you are dividing the credit card number by 100000000000000 (16 digits) so credit card numbers with 15 or less digits never pass your check of is p > 4.
The number I tested was 15 digits and p = 0.37144.

Edited 4 Years Ago by hericles: typo

okay, I've fixed the validation part. Now I'm having issues...with something else, not so trivial I hope.

I need a method called public static string getNumber(), this is what I have, which obviously doesn't work. Any ideas on this one?

using System;

public class creditcardnumbercheck
{

    static void Main(string[] args)
    {
        
       
        if (isValid(number))
        {
            Console.WriteLine("{0} is a valid credit card", number);
        }
        else
        {
            Console.WriteLine("{0} is not a valid credit card", number);
        }
    }// end Main


    /*return true if # is valid*/
    public static bool isValid(long number)
    {
        int n;
        long p;
        p = getPrefix(number, 1);
       long p2 = getPrefix(number, 2);

        if (p < 4)
        {
            if (p2 != 37)
            
            return false;
        }
       
        if (p > 6)
        {
            return false;
        }
       

        n = getSize(number);
        if (n < 15 || n > 16)
        {
            return false;
        }
        if ((sumOfDoubleEvenPlace(number) + sumOfoddPlace(number)) % 10 != 0)
        {
            return false;
        }
       
        return true;
    }

    /*Get result from step 2 in pdf*/
    public static int sumOfDoubleEvenPlace(long number)
    {
        int count, sum = 0;
        int maxCC = 16;
        for (count = 1; count <= maxCC; count++)
        {
            if (count % 2 == 0)
            {
                sum = sum + getDigit((int)(number % 10) * 2);
            }
            number /= 10;
        }
        return sum;
    }

    /*return this # if it is a single digit, otherwise, return
    * the sum of two digits */
    public static int getDigit(int number)
    {
        if (number < 10)
        {
            return number;
        }
        else
        {
            return (number / 10) + (number % 10);
        }

    }

    // return sum of odd place digits in number
    public static int sumOfoddPlace(long number)
    {
        int maxCC = 16;
        int counter, sum = 0;
        for (counter = 1; counter <= maxCC; counter++)
        {
            if (counter % 2 == 1)
            {
                sum = sum + (int)(number % 10);
            }
            number /= 10;
        }
        return sum;
    }

    // return true if the digit d is a prefix for number
    public static bool prefixMatched(long number, int dig)
    {
        if (getPrefix(number, 1) == dig || getPrefix(number, 2) == dig)
        {
            return true;
        }
        return false;
    }

    // return the number of digits in d
    public static int getSize(long dig)
    {
        int n = 0;
        while (dig != 0)
        {
            n++;
            dig /= 10;
        }
        return n;
    }

    /* return the first k number of digits from number. If the number of digits in number is less than k, return number. */
    public static long getPrefix(long number, int place)
    {
        if (place == 1)
        {
            return number / 1000000000000000;
        }
        else if (place == 2)
        {
            return number / 10000000000000;
        }
        return 0;
    }
    public static string getNumber(long number)
    {

        Console.Write("Enter credit card number: ");
        number = Convert.ToInt64(Console.ReadLine());
    }
    return number;
    }
}

Where is getNumber being called from? I imagine it would be part of the main method so the rest of the code has a number to work with but I can't see where you are calling it from.

Where is getNumber being called from? I imagine it would be part of the main method so the rest of the code has a number to work with but I can't see where you are calling it from.

lol, i figured it out....

using System;

public class creditcardnumbercheck
{

    static void Main(string[] args)
    {
        getNumber();

        if (isValid(number))
        {
            Console.WriteLine("{0} is a valid credit card", number);
        }
        else
        {
            Console.WriteLine("{0} is not a valid credit card", number);
        }
    }// end Main


    /*return true if # is valid*/
    public static bool isValid(long number)
    {
        int n;
        long p;
        p = getPrefix(number, 1);
        long p2 = getPrefix(number, 2);

        if (p < 4)
        {
            if (p2 != 37)

                return false;
        }

        if (p > 6)
        {
            return false;
        }


        n = getSize(number);
        if (n < 15 || n > 16)
        {
            return false;
        }
        if ((sumOfDoubleEvenPlace(number) + sumOfoddPlace(number)) % 10 != 0)
        {
            return false;
        }

        return true;
    }

    /*Get result from step 2 in pdf*/
    public static int sumOfDoubleEvenPlace(long number)
    {
        int count, sum = 0;
        int maxCC = 16;
        for (count = 1; count <= maxCC; count++)
        {
            if (count % 2 == 0)
            {
                sum = sum + getDigit((int)(number % 10) * 2);
            }
            number /= 10;
        }
        return sum;
    }

    /*return this # if it is a single digit, otherwise, return
    * the sum of two digits */
    public static int getDigit(int number)
    {
        if (number < 10)
        {
            return number;
        }
        else
        {
            return (number / 10) + (number % 10);
        }

    }

    // return sum of odd place digits in number
    public static int sumOfoddPlace(long number)
    {
        int maxCC = 16;
        int counter, sum = 0;
        for (counter = 1; counter <= maxCC; counter++)
        {
            if (counter % 2 == 1)
            {
                sum = sum + (int)(number % 10);
            }
            number /= 10;
        }
        return sum;
    }

    // return true if the digit d is a prefix for number
    public static bool prefixMatched(long number, int dig)
    {
        if (getPrefix(number, 1) == dig || getPrefix(number, 2) == dig)
        {
            return true;
        }
        return false;
    }

    // return the number of digits in d
    public static int getSize(long dig)
    {
        int n = 0;
        while (dig != 0)
        {
            n++;
            dig /= 10;
        }
        return n;
    }

    /* return the first k number of digits from number. If the number of digits in number is less than k, return number. */
    public static long getPrefix(long number, int place)
    {
        if (place == 1)
        {
            return number / 1000000000000000;
        }
        else if (place == 2)
        {
            return number / 10000000000000;
        }
        return 0;
    }
    public static string getNumber(long number)
    {
        {
            Console.Write("Enter credit card number: ");
            number = Convert.ToInt64(Console.ReadLine());
        }
        return number;

    }
}
This question has already been answered. Start a new discussion instead.