英文:
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 <cs50.h>
#include <stdio.h>
int main(void)
{
long ccn;
do
{
ccn = get_long("Credit Card Number: ");
}
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;
}
答案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 > 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");
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论