Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
控件可能会到达非void函数的末尾。C_C_For Loop_Compiler Errors_Cs50 - Fatal编程技术网

控件可能会到达非void函数的末尾。C

控件可能会到达非void函数的末尾。C,c,for-loop,compiler-errors,cs50,C,For Loop,Compiler Errors,Cs50,我在这段代码的返回行中得到了上述错误 // 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> vo

我在这段代码的返回行中得到了上述错误

// 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");
}
#包括
#包括
#包括
无效打印信用卡品牌(long long ccn);
bool检查有效性(长信用卡号);
int find_length(长n);
布尔校验和(长ccn);
内部主(空)
{
长信用卡号;
做
{
credit_card_number=get_long_long(“输入不带空格或连字符的有效信用卡号:”);
}同时(信用卡号<0);
如果(检查有效性(信用卡号码)=真)
打印信用卡品牌(信用卡号);
其他的
printf(“无效\n”);
}
//检查信用卡的有效性
bool检查有效性(长信用卡号)
{
int len=查找长度(信用卡号码);
返回(len==13 | | len==15 | | len==16)和校验和(信用卡号);
}
//查找卡号的长度
int查找长度(长n)
{
内伦;
对于(len=0;n!=0;n/=10,len++)
回程透镜;
}
//使用Luhn算法确定有效性
布尔校验和(长ccn)
{
整数和=0;
对于(int i=0;ccn!=0;i++,ccn/=10)
{
如果(i%2==0)
总和+=ccn%10;
其他的
{
整数位数=2*(ccn%10);
总和+=位数/10+位数%10;
}
}
返回(总和%10)==0;
}
//根据信用卡长度和前2位数字打印结果
无效打印\信用卡\品牌(长ccn)
{
如果((ccn>=34e13&&ccn<35e13)| |(ccn>=37e13&&ccn<38e13))
printf(“美国运通”);
否则如果(ccn>=51e14&&ccn<56e14)
printf(“万事达卡”);
否则如果((ccn>=4e12&&ccn<5e12)| |(ccn>=4e15&&ccn<5e15))
printf(“VISA卡”);
其他的
printf(“无效\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;
}

请注意,您应该使用无符号整数类型。否则,用户可能会输入负数,您的程序将产生错误的结果。

提示:如果n为0,您的函数将返回什么?@Alex_2539您在for循环后忘记了分号。IMHO while循环将更易于阅读,这就是为什么总是为每个循环使用大括号和if语句的原因。看起来总是一样的结构更容易理解。你的大脑会立即看到这种模式。与这种情况不同的是,你的大脑完全错过了模式。作为一种风格,确保始终使用块作为循环体有助于避免这种错误,以及其他错误。谢谢!我的循环的修订版本返回了相同的错误,但您的重写版本没有返回。非常有帮助!再次感谢!