-3

I wrote this credit card validation program where the user can enter as many didigts as long as it can be stored in type long, with the following algorithm:

  • Take every other digit in the number starting with the rightmost and add them together, num1
  • Then take the rest of the digits that were not added and double them, then split the digit and add them up. ex. if we had as one of the digits 9 we would double it to 18, then split it to 1 and 8 and add them 1+8=9 etc., num2
  • Then we take num1 and num2 and add them together, and if the resulting number ends in a zero, say 50, then the card is valid, if it ends in anything other then a zero its is invalid

My program works with a number with 1-9 digits but as soon as I try a 10 digit number the algorithm is off, I'm not sure where I'm wrong, can somebody help? Also in the if(ccnum <= 4294967295), if I enter a number greater then 4294967295 my program does not execute the else if, what is wrong?

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    unsigned long ccnum;
    int num1, num2, even, odd, divisorodd, divisoreven, first, split, check;
    double digits;

    cout << "Enter your credit card number: ";
    cin >> ccnum;

    digits = ceil(log10((double)ccnum + 1));

    if (ccnum <= 4294967295){

        num1 = 0;
        num2 = 0;
        divisorodd = 100;
        divisoreven = 10;
        first = ccnum % 10; 

        for (int i = 0; i <= digits; i++){
            odd = ((ccnum / divisorodd) % 10);
            num1 = num1 + odd;
            divisorodd = divisorodd * 100;
        }
        num1 = num1 + first;

        for (int i = 0; i <= digits; i++){
            even = ((ccnum / divisoreven) % 10) * 2;
            split = (even % 10) + ((even / 10) % 10);
            num2 = num2 + split;
            divisoreven = divisoreven * 100;
        }
    }

    else if (ccnum > 4294967295){
        cout << "\nIncorrect credit card number.\n";
    }

    cout << "\nNum1 is: " << num1 << "\n";
    cout << "\nNum2 is: " << num2 << "\n";

    check = num1 + num2;
    cout << "\nThe check number is: " << check << "\n";

    if (check % 10 == 0){
        cout << "\nThe credit card number is valid! Thank you.\n" << endl;
    }

    else if (check % 10 != 0){
        cout << "\nThe credit card number is invalid! Sorry.\n" << endl;
    }

}
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Joao Dias
  • 1
  • 4
  • 2
    `unsigned long` on Windows is 32 bits, which has a maximum value of 4294967295. What exactly do you expect to happen when you attempt to store a larger number? `ccnum <= 4294967295` is *always* going to be true, so the `else` is never going to execute. – Jonathon Reinhart Feb 25 '15 at 01:30
  • Also, a credit card number is a string of digits. It is ***not*** an integer - don't treat it as such. [Here](http://web.eecs.umich.edu/~bartlett/credit_card_number.html) is the algorithm for validating a credit card number. – Jonathon Reinhart Feb 25 '15 at 01:31
  • for the sake of the problem it has to be treated as an integer not a string, the problem states that the user can only enter an number that can be stored in type long so how do i fix it so the else executes? – Joao Dias Feb 25 '15 at 01:34
  • @JoaoDias Use a 64-bit integer such as `unsigned long long`. – PaulMcKenzie Feb 25 '15 at 01:34
  • `long` is only guaranteed to be 32 bits on any given platform. Anyone who told you that a credit card number will fit in a `long` is gravely mistaken. If you insist on using an integer, you can use `uint64_t` from ``. – Jonathon Reinhart Feb 25 '15 at 01:36
  • i will try those suggestions but does anyone know why 10 digit numbers return the wrong values – Joao Dias Feb 25 '15 at 01:41
  • @JoaoDias `does anyone know why 10 digit numbers return the wrong values` I thought the comments explained why. What is the maximum value that a 32-bit integer can hold? So you were *not* entering a 10 digit number. You hit a digit key 10 times when you entered the number, but what winds up in your long `ccnum` is *not* the number you entered. – PaulMcKenzie Feb 25 '15 at 01:44
  • @PaulMcKenzie i tried using unsigned long long and that fixes the problem with the else and makes it execute but still returns the wrong value for 10 digit numbers – Joao Dias Feb 25 '15 at 01:54
  • @JoaoDias Then it's time for you to debug your code. Second, if your number starts out as a 64-bit, you better make sure that variables that hold calculations using this number are capable of storing 64-bit values. – PaulMcKenzie Feb 25 '15 at 01:57

1 Answers1

0

Declaring the variables as unsigned long long fixed both problems

unsigned long long ccnum, num1, num2, even, odd, divisorodd, divisoreven, first, split, check;
double digits;
Joao Dias
  • 1
  • 4