I have no experience programming, and English is not my first language so I'm struggling a lot with this particular problem.
In "credit.c" you have to prompt the user for a credit card number and then check if it is invalid. If it is not, you have to provide the brand of the credit card.
I conceptually understand what I have to do, and tried to do it. First I established the number of digits in the credit card (I divided the number "n" times till the result was 0). Immediatly after that I render invalid any credit number with digits different of 13, 15 or 16.
Then I start storing the numbers of the credit card individually in an Array. Then I create an array with the "second to last" and every 2 other number (those are the ones you need to multiply times 2) Then I consider the posibility of having a resulting number greater than 9. Then I create a third array with the other numbers. Then I add the numbers and see if that addition ends in "0". If that is correct, I just check the condition of the brands (number or digits plus beggining numbers of the credit card).
Problem is the program outputs "INVALID" for every number. It says something about overflowing an integer so I imagine I messed up around the lines 30 to 40 when I go from long long to int.
Any help would be highly appreciated! This is my code
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void)
{
/* I prompt the user for a long long number */
long long numero = 0;
int contar = 0;
printf("Please type credit card number: ");
numero = get_long_long();
/* I check the number of digits by dividing by 10 till result is 0 */
while (numero != 0)
{
numero /= 10;
++contar;
}
/* If the number of digits is different than 13, 15 or 16 I know the credit card is invalid */
if (contar != 13 && contar != 15 && contar != 16)
{
printf("INVALID\n");
return 0;
}
/* I will create arrays to store the numbers I need. Will start with all the numbers */
int MiArray[contar];
long long temporal = numero;
int i = 0;
while (temporal != 0)
{
MiArray[contar - 1 - i] = temporal % 10;
}
temporal = temporal / 10;
i++;
/* Now I create the Array of the numbers I will have to multiply by 2 after */
int ArrayDeCalculo[contar / 2];
int m = 2;
int l = 0;
while ((contar - m) > -1)
{
ArrayDeCalculo[l] = 2 * MiArray[contar - m];
m = m + 2;
l++;
}
/* Here I tell the program to analyze the result of 2*every other digit and either store it or add both digits and then store it */
int DobleDigito = 0;
for (int j = 0; j < contar / 2; j++)
{
if (ArrayDeCalculo[j] > 9)
{
int PrimerDigito = 0;
DobleDigito = DobleDigito + (ArrayDeCalculo[j] % 10);
PrimerDigito = ArrayDeCalculo[j] / 10;
DobleDigito = DobleDigito + PrimerDigito;
}
else
{
DobleDigito = DobleDigito + ArrayDeCalculo[j];
}
/* Now I create the 3rd array for the numbers I haven't stored in the second array */
int TercerArray[contar - (contar / 2)];
int p = 1;
int r = 0;
while ((contar - p) > -1)
{
TercerArray[r] = MiArray[contar - p];
p = p + 2;
r++;
}
int OtroDigito = 0;
for (int w = 0; w < (contar - (contar/2)); w++)
{
OtroDigito = OtroDigito + TercerArray[w];
}
/* Now I declare the variables that will use to recover the first two digits of the original credit card (stored in the first array) */
int PrimerNumero;
int SegundoNumero;
/* Here I check Luhn's algorhitm */
if ((DobleDigito + OtroDigito) % 10 == 0)
{
PrimerNumero = MiArray[0];
SegundoNumero = MiArray[1];
}
else
{
printf("INVALID\n");
}
return 0;
/* Here I check the conditions of every brand*/
if (contar == 15 && PrimerNumero == 3 && (SegundoNumero == 4 || SegundoNumero == 7))
{
printf("AMEX\n");
}
else if (contar == 16 && PrimerNumero == 5 && (SegundoNumero >0 && SegundoNumero <6))
{
printf("MASTERCARD\n");
}
else if ((contar == 13 || contar == 16) && PrimerNumero == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
return 0;
}