控制可能到达非 void 函数的末尾。频率
control may reach end of non-void function. Frequency
我不知道为什么这个程序无法编译。它告诉我有问题,但我只是不知道这段代码有什么问题。
我正在在线上哈佛的CS50入门课程,我在编写这个函数频率时遇到了问题。我知道这可以编码得更简单,但我只是没有经验让编程更流畅和优雅。问题似乎出在函数的最后。我猜我忘记了 return 一个值,但我不知道发生了什么。
//Calculates frequency (in Hz) of a note
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
int frequency(string note);
int main(void)
{
string note = get_string();
printf("Piano Note: ");
frequency(note);
return 0;
}
int frequency(string note)
{
char key = note[0];
char accidental;
int octave;
int counter = 0;
while (note != '[=10=]')
{
counter++;
}
if(counter == 3)
accidental = note[1];
octave = note[2];
if(counter == 2)
octave = note[1];
if (counter == 3)
{
if (key == 'A')
{
if (accidental == '#')
{
if(octave == 4)
{
return 466;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 415;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
}
else if (key == 'B')
{
if (accidental == '#')
{
if(octave == 4)
{
return 523;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 466;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if (key == 'C')
{
if (accidental == '#')
{
if(octave == 4)
{
return 277;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 247;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)10/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)10/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'D')
{
if (accidental == '#')
{
if(octave == 4)
{
return 311;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 277;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'E')
{
if (accidental == '#')
{
if(octave == 4)
{
return 349;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)4/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)4/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 311;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'F')
{
if (accidental == '#')
{
if(octave == 4)
{
return 370;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}}
else if (accidental == 'b')
{
if(octave == 4)
{
return 330;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'G')
{
if (accidental == '#')
{
if(octave == 4)
{
return 415;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 370;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
}
if (counter == 2)
{
if(key == 'A')
{
if(octave == 4)
{
return 440;
}
else if(octave < 4)
{
int count = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int divisor = 0, hertz = 440;
while(divisor != count)
{
hertz /= 2;
divisor++;
}
return hertz;
}
else if(octave > 4)
{
int count = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int divisor = 0, hertz = 440;
while(divisor != count)
{
hertz *= 2;
divisor++;
}
return hertz;
}
}
else if(key == 'B')
{
if(octave == 4)
{
return 494;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)2/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)2/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if(key == 'C')
{
if(octave == 4)
{
return 262;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)9/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)9/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'D')
{
if(octave == 4)
{
return 294;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)7/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)7/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'E')
{
if(octave == 4)
{
return 330;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'F')
{
if(octave == 4)
{
return 349;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'G')
{
if(octave == 4)
{
return 392;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)2/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)2/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
}
}}}}}}}}}}}}
虽然该代码看起来很难读,但编译错误表明您的函数有一个分支,该分支将在没有 return 语句的情况下到达函数的末尾。如果看起来可行,请在函数中添加一个默认值 return,例如 int retVal = 0;然后在你的函数结束之前把行 return retVal;
我没有通读你的整个示例源代码。您可能 if/elseif/elseif/etc 没有 else 情况,它也应该 return 默认值,这将通过该编译类型错误。
如果您不想 return 默认值 return 错误代码并让调用者检查它以确保它按照您的想法进行操作。
编辑,例如,如果计数器 == 1,您的函数将不会 return 任何东西。这是编译器试图告诉你的一个例子,可能还有更多。
您需要在 printf("Piano Note: ");
之后添加 fflush(stdout);
主功能。 stdout 可能是行缓冲的,并且没有尾随
换行符,输出实际上可能不会发送到任何地方 stdout
正在运行,可能是你的屏幕。
更重要的是,下面的循环永远不会终止(假设
那张纸条 != '\0',
while (note != '[=10=]')
{
counter++;
}
if note != 0 会进入循环,但循环内什么也没有
更新注释,所以条件仍然为真,循环将
重新执行。
我在任何地方都没有看到 'string' 的定义。如果这是一个 typedef
对于 'char *',这样做是个糟糕的主意,隐藏指针
会导致混乱。如果是别的,那就难了
说出行为应该是什么。你把它比作一个字符,暗示
'string' 是 char 的 typedef,但这不是很有用,
如果你想得到一个字符串。
所以你应该丢掉 typedef,get_string() 应该 return
一个 char *,而不是 'string'。我怀疑 while 循环正在尝试
来计算字符串的长度。为此使用 strlen(3)。
我没有计算它们,但看起来你的大括号可能太多了
在末尾。当然不符合任何会
便于目测。
我不知道为什么这个程序无法编译。它告诉我有问题,但我只是不知道这段代码有什么问题。
我正在在线上哈佛的CS50入门课程,我在编写这个函数频率时遇到了问题。我知道这可以编码得更简单,但我只是没有经验让编程更流畅和优雅。问题似乎出在函数的最后。我猜我忘记了 return 一个值,但我不知道发生了什么。
//Calculates frequency (in Hz) of a note
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
int frequency(string note);
int main(void)
{
string note = get_string();
printf("Piano Note: ");
frequency(note);
return 0;
}
int frequency(string note)
{
char key = note[0];
char accidental;
int octave;
int counter = 0;
while (note != '[=10=]')
{
counter++;
}
if(counter == 3)
accidental = note[1];
octave = note[2];
if(counter == 2)
octave = note[1];
if (counter == 3)
{
if (key == 'A')
{
if (accidental == '#')
{
if(octave == 4)
{
return 466;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 415;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
}
else if (key == 'B')
{
if (accidental == '#')
{
if(octave == 4)
{
return 523;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 466;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if (key == 'C')
{
if (accidental == '#')
{
if(octave == 4)
{
return 277;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 247;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)10/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)10/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'D')
{
if (accidental == '#')
{
if(octave == 4)
{
return 311;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 277;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)8/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'E')
{
if (accidental == '#')
{
if(octave == 4)
{
return 349;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)4/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)4/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 311;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)6/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'F')
{
if (accidental == '#')
{
if(octave == 4)
{
return 370;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}}
else if (accidental == 'b')
{
if(octave == 4)
{
return 330;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if (key == 'G')
{
if (accidental == '#')
{
if(octave == 4)
{
return 415;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)1/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if (accidental == 'b')
{
if(octave == 4)
{
return 370;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
}
if (counter == 2)
{
if(key == 'A')
{
if(octave == 4)
{
return 440;
}
else if(octave < 4)
{
int count = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int divisor = 0, hertz = 440;
while(divisor != count)
{
hertz /= 2;
divisor++;
}
return hertz;
}
else if(octave > 4)
{
int count = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int divisor = 0, hertz = 440;
while(divisor != count)
{
hertz *= 2;
divisor++;
}
return hertz;
}
}
else if(key == 'B')
{
if(octave == 4)
{
return 494;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 * pow((double)2, (double)2/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 * pow((double)2, (double)2/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
}
else if(key == 'C')
{
if(octave == 4)
{
return 262;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)9/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if (octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)9/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'D')
{
if(octave == 4)
{
return 294;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)7/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)7/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'E')
{
if(octave == 4)
{
return 330;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)5/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'F')
{
if(octave == 4)
{
return 349;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)3/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
else if(key == 'G')
{
if(octave == 4)
{
return 392;
}
else if(octave > 4)
{
int count = 0, shade = 0;
for(int i = 4; i < octave; i++)
{
count++;
}
int answer = (440 / pow((double)2, (double)2/12));
while(shade != count)
{
answer *= 2;
shade++;
}
return answer;
}
else if(octave < 4)
{
int count = 0, shade = 0;
for(int i = 4; i > octave; i--)
{
count++;
}
int answer = (440 / pow((double)2, (double)2/12));
while(shade != count)
{
answer /= 2;
shade++;
}
return answer;
}
}
}
}}}}}}}}}}}}
虽然该代码看起来很难读,但编译错误表明您的函数有一个分支,该分支将在没有 return 语句的情况下到达函数的末尾。如果看起来可行,请在函数中添加一个默认值 return,例如 int retVal = 0;然后在你的函数结束之前把行 return retVal;
我没有通读你的整个示例源代码。您可能 if/elseif/elseif/etc 没有 else 情况,它也应该 return 默认值,这将通过该编译类型错误。
如果您不想 return 默认值 return 错误代码并让调用者检查它以确保它按照您的想法进行操作。
编辑,例如,如果计数器 == 1,您的函数将不会 return 任何东西。这是编译器试图告诉你的一个例子,可能还有更多。
您需要在 printf("Piano Note: ");
之后添加 fflush(stdout);
主功能。 stdout 可能是行缓冲的,并且没有尾随
换行符,输出实际上可能不会发送到任何地方 stdout
正在运行,可能是你的屏幕。
更重要的是,下面的循环永远不会终止(假设 那张纸条 != '\0',
while (note != '[=10=]')
{
counter++;
}
if note != 0 会进入循环,但循环内什么也没有 更新注释,所以条件仍然为真,循环将 重新执行。
我在任何地方都没有看到 'string' 的定义。如果这是一个 typedef 对于 'char *',这样做是个糟糕的主意,隐藏指针 会导致混乱。如果是别的,那就难了 说出行为应该是什么。你把它比作一个字符,暗示 'string' 是 char 的 typedef,但这不是很有用, 如果你想得到一个字符串。
所以你应该丢掉 typedef,get_string() 应该 return 一个 char *,而不是 'string'。我怀疑 while 循环正在尝试 来计算字符串的长度。为此使用 strlen(3)。
我没有计算它们,但看起来你的大括号可能太多了 在末尾。当然不符合任何会 便于目测。