gpt4 book ai didi

c - 为什么我总是收到无效的反馈?

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:47 25 4
gpt4 key购买 nike

在过去的 36 个小时里,我一直在研究 cs50 pset1 学分。我的代码没有产生任何错误,但是当我输入我的银行卡详细信息时,它显示无效,这可能是什么问题。我是菜鸟,请大家帮忙。

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

int main (void)
{
long long ccnumber;

do
{
//Prompting the user for his creditcard number
ccnumber=get_long_long("What is your creditcard number\n");
}
while (ccnumber<= 0);

//validating the credit card number to ascertain if its upto 13. 14 or 16
long long numbers = ccnumber;
int totaldigits = 0;

while (numbers > 0)
{
numbers = numbers/10;
totaldigits++;
}
if(totaldigits != 13 || totaldigits !=14 || totaldigits !=16)
{
printf("Invalid\n");
}

//Starting from the second to last digit
//And multiplying through by 2
int number1 = ((ccnumber/10) % 10) *2;
int number2 = ((ccnumber/1000) % 10) *2;
int number3 = ((ccnumber/100000) % 10) *2;
int number4 = ((ccnumber/10000000) % 10) *2;
int number5 = ((ccnumber/1000000000) % 10) *2;
int number6 = ((ccnumber/100000000000) % 10) *2;
int number7 = ((ccnumber/10000000000000) % 10) *2;
int number8 = ((ccnumber/10000000000000000) % 10) *2;

//Getting the Sum of the first number series
int sum1;
sum1 = (number1 + number2 + number3 + number4 + number5 + number6 + number7 + number8);

//Populating the second number series
int number9 = ccnumber % 10;
int number10 = ((ccnumber/100) % 10);
int number11 = ((ccnumber/10000) % 10);
int number12 = ((ccnumber/1000000) % 10);
int number13 = ((ccnumber/100000000) % 10);
int number14 = ((ccnumber/10000000000) % 10);
int number15 = ((ccnumber/1000000000000) % 10);
int number16 = ((ccnumber/100000000000000) % 10);

int sum2;
sum2 =(number9 + number10 + number11 + number12 + number13 + number14 + number15 + number16);

//Adding sum1 and sum2
int checksum;
checksum = sum1 + sum2;
if (checksum % 10 != 0)
{
printf("invalid/n");
}

//Verifying for visacard(1)
if(totaldigits == 13)
{
printf("Visa\n");
}

// Verifying for visa(2)
if(totaldigits == 16)
{
long long visa;
visa = ccnumber/1000000000000000;
if(visa!=4)
{
printf("Invalid\n");
}
else
{
printf("VISA\n");
}
//Verifying for MasterCard
long long master;
master = ccnumber/100000000000000;
if(master!=51 || master!=52 ||master!=53 ||master!=54 ||master!=55)
{
printf("Invalid\n");
}
else
{
printf("MASTER\n");
}
}

//Verifying for Amex
}

/计数器您的信用卡号码是多少2221000000000009无效的无效/n无效无效

最佳答案

首先,你的一些if条件是错误的。例如,

if(totaldigits != 13 || totaldigits != 14 || totaldigits != 16)

您想检查 totaldigits 是 13 还是 14 或 16,但是您在这里检查的是如果 totaldigits 不是 13,则打印无效 或者 如果 totaldigits 不是 14,则,打印无效等等。因此,它总是打印无效。您需要使用 && (AND) 而不是 || (或)

这一行也存在同样的问题。

if(master!=51 || master!=52 ||master!=53 ||master!=54 ||master!=55)

另一个问题是你把 1 不必要的 0 放在这一行的除法中,所以它总是产生 0。

int number8 = ((ccnumber/10000000000000000) % 10) *2; // 1 zero unnecessary

正如评论中所建议的,您可能需要考虑创建一个数组来存储单个数字,而不是使用 16 个不同的整数。当你有一个数组时,你可以在 for 循环中进行除法,这样你就不会与很多零混淆。作为示例,您可以引用此链接 A solution for cs50's credit card validation problem .

关于c - 为什么我总是收到无效的反馈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58634475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com