Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #70284

Re: Improving the program, issues with logical operators, incorrect output

From gordonb.papec@burditt.org (Gordon Burditt)
Subject Re: Improving the program, issues with logical operators, incorrect output
Newsgroups comp.lang.c
References <cd2ea66c-053e-4ba5-8704-211f2dd5609b@googlegroups.com>
Message-ID <K9-dndp5nfPWzm7InZ2dnUU7-RmdnZ2d@posted.internetamerica> (permalink)
Date 2015-09-11 17:18 -0500

Show all headers | View raw


> 1) Get the card number from the user.

Bank card numbers can go up to 19 digits.  (Look up "bank card
number" on Wikipedia.)  Don't forget about space for the trailing
'\0'.

Also, someone malicious will try entering something vastly overlong
(gigabytes?) with the hope of getting free money or free merchandise
or getting a fake credit card number accepted for real.

scanf() used with a %s format makes it impossible to protect
against overlong input.

> 2) Multiply every other digit by 2,
> starting with the number's second-to-last digit,
> and then add those products' digits together.
> 
> 3) Add the sum to the sum of the digits that
> weren't multiplied by 2.
> 
> 4) If the total's last digit is 0 (or, put more
> formally, if the total modulo 10 is congruent to
> 0), the number is valid.

This is commonly known as the Luhn algorithm or Luhn checksum.  It's
good as a quick check for mistyping a digit or swapping two digits
(catches most such errors) but is no substitute for checking online
that the credit card account is active and in good standing and not
reported stolen.  This, of course, requires a merchant account and
each query is likely to cost money (possibly only a few cents).
This can add up if you are teaching a large class and let your students
use the merchant account for testing their programs.  It can also
add up if you connect this to a web site and let any credit card
number thief to validate credit cards using your web site.

> 5) If the card is valid, print that it's a valid
> card and the name of the issuer. 

Define valid card!  "Passes the Luhn checksum", "Has a valid issuer
(from a list of accepted issuers)", "Has the correct number of
digits for the issuer", "Is a credit card", "is not reported stolen",
and "all of the above" are all different criteria.

> The name of issuer depends on the following:
> 
> American Express uses 15-digit numbers, 
> MasterCard uses 16-digit numbers, and
> Visa uses 13- and 16-digit numbers.

You have left off many issuers.  And, this is important, many
issuers issue cards that are NOT credit cards!

Kroger loyalty cards have a prefix of 6032 and use 15 digits.
Lowe's loyalty cards have a prefix of 6006 and use 19 digits.
A Texas driver's license doesn't seem to have any number on it that
isn't part of the address.  (There may be other information on
tracks the keyboard-with-card-reader I'm using doesn't read.)


> American Express numbers all start with 34 or 37; 
> MasterCard numbers all start with 51, 52, 53, 54, or 55; 
> and Visa numbers all start with 4.

Discover starts with 6011, 62, 64, or 65, although they don't necessarily
own all the numbers in those blocks, and uses 16-digit numbers.

Mastercard has announced it's going to start using numbers in a block
that begins with 2 starting sometime in 2016.

So, what should the program do if the Luhn checksum passes but:
	(a) The number matches no known issuer (e.g. begins with 0, 1,
	    2, 6, 7, 8, or 9, or 3 or 5 but the number is outside the
	    indicated blocks above)
	(b) The number matches an issuer but the number of digits is wrong.
	(c) The "number" contains non-numeric characters (recently one
	    payee complained that my credit card number read as random 
	    letters.  It read correctly the next try and nobody else
	    has a problem with it.  Apparently it's possible for a
	    card to be misread.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Improving the program, issues with logical operators, incorrect output Alla _ <modelling.data@gmail.com> - 2015-09-08 06:47 -0700
  Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-08 07:23 -0700
    Re: Improving the program, issues with logical operators, incorrect output Alla _ <modelling.data@gmail.com> - 2015-09-08 07:25 -0700
      Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-08 07:44 -0700
      Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-08 17:04 +0100
      Re: Improving the program, issues with logical operators, incorrect output Keith Thompson <kst-u@mib.org> - 2015-09-08 09:21 -0700
  Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-08 18:32 +0100
  Re: Improving the program, issues with logical operators, incorrect output Barry Schwarz <schwarzb@dqel.com> - 2015-09-08 15:53 -0700
  Re: Improving the program, issues with logical operators, incorrect output bartekltg <bartekltg@gmail.com> - 2015-09-09 15:42 +0200
  Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-09 16:11 +0200
    Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-09 16:01 +0100
      Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-09 21:11 +0200
        Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-09 20:42 +0100
          Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-09 22:23 +0200
          Re: Improving the program, issues with logical operators, incorrect output Keith Thompson <kst-u@mib.org> - 2015-09-09 13:41 -0700
            Re: Improving the program, issues with logical operators, incorrect output Richard Heathfield <rjh@cpax.org.uk> - 2015-09-09 22:21 +0100
            Re: Improving the program, issues with logical operators, incorrect output Les Cargill <lcargill99@comcast.com> - 2015-09-09 18:25 -0500
    Re: Improving the program, issues with logical operators, incorrect output Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-09 12:41 -0700
      Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-09 22:24 +0200
      Re: Improving the program, issues with logical operators, incorrect output Keith Thompson <kst-u@mib.org> - 2015-09-09 13:34 -0700
        Re: Improving the program, issues with logical operators, incorrect output Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-09 16:25 -0700
          Re: Improving the program, issues with logical operators, incorrect output Keith Thompson <kst-u@mib.org> - 2015-09-09 17:38 -0700
            Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-12 00:48 -0700
              Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-12 00:55 -0700
              Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-12 14:50 +0200
                Re: Improving the program, issues with logical operators, incorrect output fir <profesor.fir@gmail.com> - 2015-09-12 07:12 -0700
        Re: Improving the program, issues with logical operators, incorrect output David Brown <david.brown@hesbynett.no> - 2015-09-10 09:56 +0200
  Re: Improving the program, issues with logical operators, incorrect output gordonb.papec@burditt.org (Gordon Burditt) - 2015-09-11 17:18 -0500
    Re: Improving the program, issues with logical operators, incorrect output Barry Schwarz <schwarzb@dqel.com> - 2015-09-13 01:08 -0700
    Re: Improving the program, issues with logical operators, incorrect output Stephen Sprunk <stephen@sprunk.org> - 2015-09-13 19:03 -0500

csiph-web