0

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

0

The ^ operator is bitwise XOR, so you're saying while (n >= 6 || n <= 26), which makes your do..while loop infinite. Based on the credit specs, I don't see why you would need that loop at all.

o / 100 divides, but otherwise doesn't do anything. You probably meant o /= 100, which assigns the result to the variable divided. Same for p / 100, another infinite loop. And I'd check for > 0 rather than > 99.

It might be o = n / 10 to start at the second-to-last digit. And the doubling seems done wrong. If the doubling yields a two-digit number, you'd add the digits.

Also, count == 12 || 15 && first == 4 is wrong in multiple ways: First, it would have to be count == 12 || count == 15. Then, && has higher precedence than ||, so you need parentheses, like (count == 12 || count == 15) && first == 4.

Umm... and I think it should be count == 13 and count == 16.

Is every number starting with 3 AMEX? Like 380000000000000? Or 5900000000000000 a valid MASTERCARD? I just made them up, so hope I got the checksum right.

And the output strings should be MASTERCARD and INVALID, not MC (Hammer?) and Credit Card number NOT valid. And you forgot the case of a valid checksum, but no known card type. Read the specs carefully. That's a very important thing. Sometimes even copy&paste strings from the specs to not include any typos.

PS: Unless you're into ASM or Basic, you probably never form a loop with if or equivalent. So don't talk about "If Loops".

6
  • Thank YOU Blauelf. This gives me tons of clarity - and some things to work on. Kudos! Apr 24 '18 at 17:26
  • Blauelf - if I may... Two questions: If (i++) would add 1 to the value of i, why could I not use i/100? It would divide the value of I by 100 - thereby changing its value? Apr 25 '18 at 19:09
  • Sorry - I hit enter.... Blauelf - if I may... Two questions: If a++ would add 1 to the value of a, why could I not use a/100? It would divide the value of a by 100 - thereby changing its value? Also, how can you write square root math in C? I have looked for a square root operator and found sometime x^10 and also have found bitwise operator x ^ 10 and thought the difference was that there was a space between the elements. Apr 25 '18 at 19:15
  • Spaces are ignored. ^ often is used to describe superscript, like the power, but not as a C operator. Likewise, _ is often used to describe subscript. / calculates, but does not change anything, while /= combines / and =. ++ can be irritating, as it has both value and side-effect, using ++a is equivalent to a += 1, the latter being more readable. There is no such equivalent to a++, although the difference might not be relevant in your case.
    – Blauelf
    Apr 26 '18 at 6:56
  • So...... Is there an operator that will give a square root? If so, what is it? I can't find it anywhere on the internet. Grrrr. Apr 30 '18 at 1:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .