控件可以到达C中非空函数的末尾

控件可以到达C中非空函数的末尾,c,cs50,C,Cs50,我有这个错误,我无法调试它 helpers.c:136:1:错误:控件可能到达非void函数的末尾 IDE在} 这是我的密码 // Calculates frequency (in Hz) of a note int frequency(string note) { // TODO if (strlen(note) == 2) { if (note[0] == 'A') { int freq = round(440

我有这个错误,我无法调试它

helpers.c:136:1:错误:控件可能到达非void函数的末尾

IDE在}

这是我的密码

// Calculates frequency (in Hz) of a note
int frequency(string note)
{
    // TODO
    if (strlen(note) == 2)
    {
        if (note[0] == 'A')
        {
            int freq = round(440 * (pow(2, (((int) note[1]) - 52))));
            return freq;
        }
    else
    {
        if (note[0] == 'A')
        {
            if (note[1] == '#')
            {
                int freq = round(466 * (pow(2, (((int) note[2]) - 52))));
                return freq;
            }
            else if (note[1] == 'b')
            {
                int freq = round(415 / (pow(2, (((int) note[2]) - 52))));
                return freq;
            }
        }
   }
}

您需要返回所有代码路径。让我们看一下您的示例:

int frequency(string note)
{
    // TODO
    if (strlen(note) == 2)
    {
        if (note[0] == 'A')
        {
            int freq = round(440 * (pow(2, (((int) note[1]) - 52))));
            return freq;
        }
        // return what?
    else
    {
        if (note[0] == 'A')
        {
            if (note[1] == '#')
            {
                int freq = round(466 * (pow(2, (((int) note[2]) - 52))));
                return freq;
            }
            else if (note[1] == 'b')
            {
                int freq = round(415 / (pow(2, (((int) note[2]) - 52))));
                return freq;
            }
            // return what?
        }
        // return what?
   }
}
int frequency(string note)
{
    int freq = 0;
    // TODO
    if (strlen(note) == 2)
    {
        if (note[0] == 'A')
        {
            freq = round(440 * (pow(2, (((int) note[1]) - 52))));
        }
    else
    {
        if (note[0] == 'A')
        {
            if (note[1] == '#')
            {
                freq = round(466 * (pow(2, (((int) note[2]) - 52))));
            }
            else if (note[1] == 'b')
            {
                freq = round(415 / (pow(2, (((int) note[2]) - 52))));
            }
        }
   }

   return freq;
}
一个选项是在代码块的开头用默认值声明
freq
。然后根据需要的条件修改该值,然后在函数末尾返回该值。例如:

int frequency(string note)
{
    // TODO
    if (strlen(note) == 2)
    {
        if (note[0] == 'A')
        {
            int freq = round(440 * (pow(2, (((int) note[1]) - 52))));
            return freq;
        }
        // return what?
    else
    {
        if (note[0] == 'A')
        {
            if (note[1] == '#')
            {
                int freq = round(466 * (pow(2, (((int) note[2]) - 52))));
                return freq;
            }
            else if (note[1] == 'b')
            {
                int freq = round(415 / (pow(2, (((int) note[2]) - 52))));
                return freq;
            }
            // return what?
        }
        // return what?
   }
}
int frequency(string note)
{
    int freq = 0;
    // TODO
    if (strlen(note) == 2)
    {
        if (note[0] == 'A')
        {
            freq = round(440 * (pow(2, (((int) note[1]) - 52))));
        }
    else
    {
        if (note[0] == 'A')
        {
            if (note[1] == '#')
            {
                freq = round(466 * (pow(2, (((int) note[2]) - 52))));
            }
            else if (note[1] == 'b')
            {
                freq = round(415 / (pow(2, (((int) note[2]) - 52))));
            }
        }
   }

   return freq;
}
“我有这个错误,我无法调试它”

为什么你不能调试它?我认为这是所有编译器消息中最直接和最明显的消息之一

简单想想:
如果strlen(note)不是
2
(比如说,
3
7

那么如果
note[0]
不是
'A'
(比如,
'B'
'C'
),接下来会发生什么

那么您的函数将返回什么


回答:
您的代码将到达函数的末尾,必须返回一个
int
,但不会返回任何内容

如果
note
“B#
?请注意,如果长度小于2,则将执行第二个分支,这将导致更未定义的行为。什么是
字符串
?不要将指针隐藏在typedefslooking后面的“related”列表中,我们可能需要一个规范的q/a来显示此特定编译器警告。代码中有不匹配的大括号,这表明这可能不是您实际尝试编译的代码。