this the cs50 problem in question :
credit
the logic in my checksum seem to be ill constructed as it doesn’t recognize 4222222222222 as valid even though it sums is 40 and falsely indicates
4111111111111113 as visa even though it sums is 28
other than these two it labels any other credit card correctly
this is my code :
#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
bool digit(string str, int buffer);
int numofdigits(long long card_num);
int fst_ndigit(long long credit, int buffer, int numdigit);
int main(void)
{
long long credit = get_long("please enter your credit card number : ");
//Note:
//using a boolean variable can reduce repetition of " INVALID " message
bool valid = true;
if (credit < (long long) pow(10, 13) || credit > (long long) pow(10, 16))
{
printf("INVALIDn");
valid = false;
}
int buffer = numofdigits(credit);
long long aux = credit;
int last_two;
int last_one;
//Note: variable names cannot start with numbers!
int secondlast_one;
int mul = 1;
int sum1 = 0;
int sum2 = 0;
if(valid){
for(int i = 0; aux > 0; i++)
{
//1st step :
//Multiply every other digit by 2, starting with the number’s second-to-last digit,
//and then add those products’ digits together.
last_two = aux % (long long) pow(10, buffer-2);
last_one = last_two % 10;
secondlast_one = last_two / 10;
mul = secondlast_one*2;
if((mul / 10) != 0)
{
sum1 += (mul / 10) + (mul % 10);
}
else
{
sum1 += mul;
}
//2nd step:
//Add the previous sum to the sum of the digits that weren’t multiplied by 2.
sum2 += last_one;
aux = aux / 100;
}
//3rd step:
//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!
if((sum1 + sum2) % 10 ==0)
{
valid = true;
}
int fst_two = fst_ndigit(credit, buffer, 2);
int fst_one = fst_ndigit(credit, buffer, 1);
char str[17];
// Note :
// sprintf(str, "%d", (int)credit);
// this line is incorrect because the prog will spit out a random integer
sprintf(str, "%lld", credit);
// lld is format for long long int
if (digit(str, buffer) && valid)
{
if (buffer == 15 && (fst_two == 34 || fst_two == 37))
{
printf("AMEXn");
}
else if (buffer == 16 && (fst_two == 51 || fst_two == 52 || fst_two == 53 || fst_two == 54 || fst_two == 55))
{
printf("MASTERCARDn");
}
else if ((buffer == 13 || buffer == 16) && fst_one == 4)
{
printf("VISAn");
}
else
{
printf("INVALIDn");
}
}
else
{
printf("INVALIDn");
}
}
}
int numofdigits(long long card_num)
{
int count = 0;
while (card_num > 0)
{
card_num /= 10;
count++;
}
return count;
}
bool digit(string str, int buffer)
{
bool all_digits = true;
for (int i = 0; i < buffer; i++)
{
if (!isdigit(str[i]))
{
all_digits = false;
break;
}
}
return all_digits;
}
int fst_ndigit(long long credit, int buffer, int numdigit)
{
int fstndigit = (int) (credit / (long long) pow(10, buffer - numdigit));
return fstndigit;
}
as you see in the checksum section:
int buffer = numofdigits(credit);
long long aux = credit;
int last_two;
int last_one;
//Note: variable names cannot start with numbers!
int secondlast_one;
int mul = 1;
int sum1 = 0;
int sum2 = 0;
if(valid){
for(int i = 0; aux > 0; i++)
{
//1st step :
//Multiply every other digit by 2, starting with the number’s second-to-last digit,
//and then add those products’ digits together.
last_two = aux % (long long) pow(10, buffer-2);
last_one = last_two % 10;
secondlast_one = last_two / 10;
mul = secondlast_one*2;
if((mul / 10) != 0)
{
sum1 += (mul / 10) + (mul % 10);
}
else
{
sum1 += mul;
}
//2nd step:
//Add the previous sum to the sum of the digits that weren’t multiplied by 2.
sum2 += last_one;
aux = aux / 100;
}
//3rd step:
//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!
if((sum1 + sum2) % 10 ==0)
{
valid = true;
}
in a loop that goes on until all digits of the credit card number is checked, I tried to extract the last digit and the second to last then multiplied the latter by 2 and added to sum1 and if it surpasses 9, I add each of its digits indiviually then sum2 is the sum of every other digit beginnig with the last digit
finally out of the loop:
if total = sum1 + sum2 have as last digit 0 then the credit car number is valid other than that invalid
can someone please help me ?
abeginner1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.