gpt4 book ai didi

c - 控制可能会达到非无效功能的终点。 C

转载 作者:行者123 更新时间:2023-12-02 10:44:49 25 4
gpt4 key购买 nike

我在此代码的返回行上收到上述错误。

// Finds the length of the card number 
int find_length(long long n)
{
int len;
for (len = 0; n != 0; n /= 10, len++)
return len;
}
完整代码:
#include <stdio.h>
#include <cs50.h>
#include <unistd.h>



void print_credit_card_brand(long long ccn);
bool check_validity(long long credit_card_number);
int find_length(long long n);
bool checksum(long long ccn);



int main(void)
{
long long credit_card_number;
do
{
credit_card_number = get_long_long("Enter valid credit card number without spaces or hyphens: ");
} while (credit_card_number < 0);

if (check_validity(credit_card_number) == true)
print_credit_card_brand(credit_card_number);
else
printf("INVALID\n");
}



// Checks validity of credit card
bool check_validity(long long credit_card_number)
{
int len = find_length(credit_card_number);
return (len == 13 || len == 15 || len == 16) && checksum(credit_card_number);
}



// Finds the length of the card number
int find_length(long long n)
{
int len;
for (len = 0; n != 0; n /= 10, len++)
return len;
}



// Determines validity using Luhn’s Algorithm
bool checksum(long long ccn)
{
int sum = 0;
for (int i =0; ccn != 0; i++, ccn /= 10)
{
if (i % 2 == 0)
sum += ccn % 10;
else
{
int digit = 2 * (ccn % 10);
sum += digit / 10 + digit % 10;
}
}
return (sum % 10) == 0;
}



// Prints results besed on length of credit card and 1st 2 digits
void print_credit_card_brand(long long ccn)
{
if ( (ccn >= 34e13 && ccn < 35e13) || (ccn >= 37e13 && ccn < 38e13) )
printf("AMEX\n");
else if (ccn >= 51e14 && ccn < 56e14)
printf("MASTERCARD\n");
else if ( (ccn >= 4e12 && ccn < 5e12) || (ccn >= 4e15 && ccn < 5e15) )
printf("VISA\n");
else
printf("INVALID\n");
}

最佳答案

您忘记在for循环后放置分号。所以用这种错字功能看起来像

int find_length(long long n)
{
int len;
for (len = 0; n != 0; n /= 10, len++)
{
return len;
}
}
该函数可以例如通过以下方式查找
unsigned int find_length( unsigned long long n )
{
const unsigned long long Base = 10;
unsigned int len = 0;

for ( ; n != 0; n /= Base )
{
++len;
}

return len;
}
请注意,您应该使用无符号整数类型。否则,用户可以输入一个负数,并且您的程序将产生错误的结果。

关于c - 控制可能会达到非无效功能的终点。 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64102586/

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