只在我的代码中接收到无效的信用 CS50。

huangapple go评论56阅读模式
英文:

Only receiving invalid in my code for Credit CS50

问题

以下是您提供的代码的翻译部分:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    long ccn;

    do
    {
        ccn = get_long("信用卡号码:");
    }
    while (ccn < 0);

    int totalsum = 0;
    int digit = 0;
    while (ccn > 0)
    {
        if (digit % 2 == 1)
        {
            int ccn2 = ccn % 10 * 2;
            totalsum += (ccn2 % 10) + (ccn2 / 10);
        }
        else if (digit % 2 == 0)
        {
            totalsum += ccn % 10;
        }
        ccn /= 10;
        digit++;
    }

    if ((totalsum % 10) != 0)
    {
        printf("%s\n", "INVALID");
        return 0;
    }

    long visa = ccn;
    long americanexpress = ccn;
    long mastercard = ccn;

    while (americanexpress >= 10000000000000)
    {
        americanexpress /= 10000000000000;
    }
    if (digit == 15 && (americanexpress == 34 || americanexpress == 37))
    {
        printf("%s\n", "AMEX");
        return 0;
    }

    while (mastercard >= 100000000000000)
    {
        mastercard /= 100000000000000;
    }
    if (digit == 16 && (mastercard >= 51 && mastercard <= 55))
    {
        printf("%s\n", "MASTERCARD");
        return 0;
    }

    while (visa > 10)
    {
        visa /= 10;
    }
    if (visa == 4 && (digit == 13 || digit == 16))
    {
        printf("%s\n", "VISA");
        return 0;
    }
    else
    {
        printf("%s\n", "INVALID");
    }
    return 0;
}

这是您提供的代码的中文翻译部分。如果您需要任何其他帮助,请随时提出。

英文:

When I run my code and input card numbers, it keeps returning as invalid. I tried to find out what was wrong with my code by checking if it prints out the right # of digits or if the checksum is correct, but everything seems fine. I'm a coding newbie so there might be things I overlooked.

The code is supposed to print out if the credit card number is a VISA/American Express/Mastercard, else it is invalid. But somehow, the output is always invalid. The code validates the card number through Luhn's Algorithm:

1.) Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
2.) Add the sum to the sum of the digits that weren’t multiplied by 2.
3.) 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!

American Express starts with 35 or 37 and uses 15 digit numbers, MasterCard starts with 51, 52, 53, 54, or 55 and uses 16 digit numbers, and Visa starts with 4 and uses 13 and 16 digit numbers.

I verified the checksum part of my code, and it seemed to be functioning fine. I verified this through printing out the digits and total sum of a card number:

Example:
378282246310005 (American Express)
Digits: 15
Sum: 60

So is the one that checks if the total sum's last digit is 0. I believe the problem lies after that part, as when I type in a card number whether it belongs to VISA/American Express/Mastercard, I get "INVALID".

My Attempts:
378282246310005 (American Express)
"INVALID"

4012888888881881 (VISA)
"INVALID"

5555555555554444 (Mastercard)
"INVALID"

Any help is greatly appreciated!

#include &lt;cs50.h&gt; 
#include &lt;stdio.h&gt;
int main(void) 
{ 
long ccn;
do 
{
ccn = get_long(&quot;Credit Card Number: &quot;);
}
while (ccn &lt; 0);
int totalsum = 0;
int digit = 0;
while (ccn &gt; 0)
{
if (digit % 2 == 1)
{
int ccn2 = ccn % 10 * 2;
totalsum += (ccn2 % 10) + (ccn2 / 10);
}
else if (digit % 2 == 0)
{
totalsum += ccn % 10;
}
ccn /= 10;
digit++;
}
if ((totalsum % 10) != 0)
{
printf(&quot;%s\n&quot;, &quot;INVALID&quot;);
return 0;
}
long visa = ccn;
long americanexpress = ccn;
long mastercard = ccn;
while (americanexpress &gt;= 10000000000000) 
{
americanexpress /= 10000000000000;
}
if (digit == 15 &amp;&amp; (americanexpress == 34 || americanexpress == 37))
{
printf(&quot;%s\n&quot;, &quot;AMEX&quot;);
return 0;
}
while (mastercard &gt;= 100000000000000)
{
mastercard /= 100000000000000;
}
if (digit == 16 &amp;&amp; (mastercard &gt;= 51 &amp;&amp; mastercard &lt;= 55))
{
printf(&quot;%s\n&quot;, &quot;MASTERCARD&quot;);
return 0;
}
while (visa &gt; 10)
{
visa /= 10;
}
if (visa == 4 &amp;&amp; (digit == 13 || digit == 16))
{ 
printf(&quot;%s\n&quot;, &quot;VISA&quot;);
return 0;
}
else
{
printf(&quot;%s\n&quot;, &quot;INVALID&quot;);
}
return 0;
}

答案1

得分: 1

After this loop

while (ccn > 0)
{
    /* ... */

    ccn /= 10;
}

ccn will be zero. Afterwards the value of ccn is used to initialize three variables:

long visa = ccn;
long americanexpress = ccn;
long mastercard = ccn;

So any calculations done with these variables will be operating on zero (e.g., while (visa > 10)).

Move the definitions of these variables above the loop to save the initial value of ccn:

long visa = ccn;
long americanexpress = ccn;
long mastercard = ccn;

while (ccn > 0)
{
    /* ... */

    ccn /= 10;
}

Aside: printf("%s\n", "INVALID"); is more simply written as puts("INVALID");.

英文:

After this loop

while (ccn &gt; 0)
{
    /* ... */

    ccn /= 10;
}

ccn will be zero. Afterwards the value of ccn is used to initialize three variables:

long visa = ccn;
long americanexpress = ccn;
long mastercard = ccn;

So any calculations done with these variables will be operating on zero (e.g., while (visa &gt; 10)).

Move the definitions of these variables above the loop to save the initial value of ccn:

long visa = ccn;
long americanexpress = ccn;
long mastercard = ccn;

while (ccn &gt; 0)
{
    /* ... */

    ccn /= 10;
}

Aside: printf(&quot;%s\n&quot;, &quot;INVALID&quot;); is more simply written as puts(&quot;INVALID&quot;);.

huangapple
  • 本文由 发表于 2023年7月18日 10:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定