Skip to main content
Subtract '0', not 0
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

To get the actual numeric value of a character you would subtract 0'0' from the numeric character. For a single character this would always give you a value between zero and nine.

To get the actual numeric value of a character you would subtract 0 from the numeric character. For a single character this would always give you a value between zero and nine.

To get the actual numeric value of a character you would subtract '0' from the numeric character. For a single character this would always give you a value between zero and nine.

Based on comment removed suggestion for test of 15 or 16 characters.
Source Link
pacmaninbw
  • 24.8k
  • 11
  • 45
  • 109

It might be better to treat the credit card number as a string. In the C programming language a string is a null terminated array of type char or character. This would remove all the division in the program to get each character. It would also allow the program to detect if any non-numeric characters were entered. The first test to see if it is valid would then be to check if there are 15 or 16 numeric characters in the string, all valid credit card numbers have either 15 or 16 numbers in them (some AMEX cards have 15 numeric characters, all other credit cards have 16 numeric characters).

It might be better to treat the credit card number as a string. In the C programming language a string is a null terminated array of type char or character. This would remove all the division in the program to get each character. It would also allow the program to detect if any non-numeric characters were entered. The first test to see if it is valid would then be to check if there are 15 or 16 numeric characters in the string, all valid credit card numbers have either 15 or 16 numbers in them (some AMEX cards have 15 numeric characters, all other credit cards have 16 numeric characters).

It might be better to treat the credit card number as a string. In the C programming language a string is a null terminated array of type char or character. This would remove all the division in the program to get each character. It would also allow the program to detect if any non-numeric characters were entered.

Added conditional assignment.
Source Link
pacmaninbw
  • 24.8k
  • 11
  • 45
  • 109

There is a second form of the if statement that could also make the code shorter, it is generally covered in the later part of any C programming course

    tN = (tN > 9)? tN - 9 : tN;

This single statement is equivalent to the function above.

A second example of repeating code is the division to reduce each digit in the credit card number to a single number, this could also be put into a loop. The divisor could be reduced in each iteration of the loop if the algorithm sticks with using numbers.

A second example of repeating code is the division to reduce each digit in the credit card number to a single number, this could also be put into a loop. The divisor could be reduced in each iteration of the loop if the algorithm sticks with using numbers.

There is a second form of the if statement that could also make the code shorter, it is generally covered in the later part of any C programming course

    tN = (tN > 9)? tN - 9 : tN;

This single statement is equivalent to the function above.

A second example of repeating code is the division to reduce each digit in the credit card number to a single number, this could also be put into a loop. The divisor could be reduced in each iteration of the loop if the algorithm sticks with using numbers.

Source Link
pacmaninbw
  • 24.8k
  • 11
  • 45
  • 109
Loading