In testing out your code utilizing the good comments above regarding the use of "long long" designations for your integer variables, following was the initial refactoring of the code to pass and use "long long" variables in the validation function.
First off, in the main function, the credit card number was refactored to be a "long long" variable.
int main(void)
{
long long creditCardNo;
do
{
creditCardNo = get_long_long("Enter credit card number: ");
}
Then, the function was also refactored in the same way.
bool validation(long long number) /* Refactored to handle large credit card values */
{
long i = 10;
long j = 1;
int totalMultiplied = 0;
int totalNotMultiplied = 0;
while (number / i != 0)
{
totalMultiplied += ((number / i) % 10) * 2;
printf("totalMultiplied: %d\n", totalMultiplied); /* Using printf as a manual debug tool */
i *= 100;
}
while (number / j != 0)
{
totalNotMultiplied += (number / j) % 10;
printf("totalNotMultiplied: %d\n", totalNotMultiplied); /* Using printf as a manual debug tool */
j *= 100;
}
if ((totalMultiplied + totalNotMultiplied) % 10 == 0)
{
return true;
}
else
{
return false;
}
}
While that did address the overflow issues, there was still some refinement of the check digit calculation that needed to be performed in the function as testing of the program with a "technically valid" Amex credit card number came back as invalid.
craig@Vera:~/C_Programs/Console/ValidCC/bin/Release$ ./ValidCC
Enter credit card number: 350000000000006
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 10
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 9
INVALID
Further investigation as to how the Luhn check digit algorithm works, there needs to be accommodation when the resulting calculated value exceeds a value of "9" as noted in the following link:
"https://en.wikipedia.org/wiki/Luhn_algorithm"
With that in mind, another refactoring refinement was done to mimic the Luhn check digit algorithm.
bool validation(long long number) /* Refactored to handle large credit card values */
{
long i = 10;
long j = 1;
int totalMultiplied = 0;
int totalNotMultiplied = 0;
while (number / i != 0)
{
if (((number / i % 10) < 5)) /* Luhn test */
totalMultiplied += ((number / i) % 10) * 2;
else
totalMultiplied += ((number / i) % 10) * 2 - 9;
printf("totalMultiplied: %d\n", totalMultiplied); /* Using printf as a manual debug tool */
i *= 100;
}
while (number / j != 0)
{
totalNotMultiplied += (number / j) % 10;
printf("totalNotMultiplied: %d\n", totalNotMultiplied); /* Using printf as a manual debug tool */
j *= 100;
}
if ((totalMultiplied + totalNotMultiplied) % 10 == 0)
{
return true;
}
else
{
return false;
}
}
Following is the resulting terminal output utilizing the same test credit card number.
craig@Vera:~/C_Programs/Console/ValidCC/bin/Release$ ./ValidCC
Enter credit card number: 350000000000006
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 0
totalMultiplied: 1
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 6
totalNotMultiplied: 9
AMEX
This additional refinement was to be preemptive to avoid further confusion in testing out the program.
The takeaway from all of this is to continue delving into "C" tutorials to get a better understanding of the limitations of the various variable definitions as well as getting properly familiar with the utilization of validation processes such as check digit derivation.
iorjare overflowinglongcan hold numbers with 15 digits (48+ bits)?longfornumberand you dividenumber / iuntil the result is 0, you must be prepared thatigets larger thannumber, which means you need to uselongforias well. And you still risk thati *= 100overflows.