I have come up with, what seems to me, to be a logical walkthrough of the CREDIT problem (after banging my head against a wall for too long) - but the code does not make sense to the compiler. And I grant that some things need to be rewritten. Sorry to add this question to the millions already posed on this problem, but if someone has some hints - I'd be eternally grateful.
Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
/*this first loop requires user input of a long_long
with a length of at least 13 digits (10^12) and a
maximum of 16 digits (10^16)*/
{
long long n = 0;
long long o = 0;
long long l = 0;
int g = 10;
int suma = 0;
int sumb = 0;
int sumfin = 0;
int count = 0;
int counta = 0;
int countb = 0;
do
{
printf("Enter credit card number: ");
n = get_long_long();
}
while (n >= (10^12) || n <= (10^16));
// Code to find first digit to prove Credit Card Company
// Found https://codeforwin.org/2015/06/how-to-find-first-and-last-digit-of-any-number.html
long long first = n;
// Remove last digit from number till only one digit is left
while(first >= 10)
{
first = first / 10;
}
// This code will compute Luhn's Algorithm:
for (o = n; o > 99; o / 100)
{
l = (o / g) %10;
suma = (l * 2) + suma;
}
for (long long p = n; p > 99; p / 100)
{
int q = p % 10;
sumb = q + sumb;
}
sumfin = suma + sumb;
if (sumfin % 10 == 0)
{
// The following code will give the number of digits in long long n
// Code was learned from https://www.programiz.com/c-programming/examples/digits-count
while(n != 0)
{
// n = n/10
n /= 10;
++count;
}
// The following will require number of digits to verify that it
// is a real credit card number 13, 15 or 16 digits. Credit for code:
// https://stackoverflow.com/questions/9721042/count-number-of-digits-which-method-is-most-efficient
if (count == 12 || 15 && first == 4)
{
Printf("VISA\n");
}
else (count == 14 && first == 3)
{
Printf("AMEX\n");
}
else (count == 15 && first == 5)
{
Printf("MC\n");
}
}
else
{
printf("Credit Card number NOT valid\n");
}
return 0;
}
I seem to be getting my first error at the for loops for Luhn's Algorithm. And then in the If Loops to prove form what company the card is issued.
Thanks!
Tufnel