I have problem with my code (I guess). I have got the control reaches end of non-void function error even though my ifs (i think so ;) ) are covering all of the possibilities.
Please have a look:
int frequency(string note)
{
int n = 0; // fraction(?) counter (n/12)
int hz = 0; //Hz of A in the Octave
int noteHz = 0;
bool specialCase = false;
float temp = 0;
//2nd step: calculate the A hrtz of octave
if (note[strlen(note) - 1] < 4)
{
for (int i = 1; 4 - i != strlen(note); i++)
hz /= 2;
}
else if(note[strlen(note) - 1] > 4)
{
for (int j = 1; 4 + j != strlen(note); j++)
hz *= 2;
}
else
hz = 440;
//!!!!THIS NEEEDS TO COME FOR AN END TO BE BETTER IMPLEMENTED - RIGHT NOW ITS BUGGY!!!!
//1st step: check if we do have special representation of notes or "normal"
if (note[1] == '#' && note[0] == 'B')//- this was my 1st code in this place this below (code with use of strncmp):
{
hz *=2;
specialCase = true;
}
else if (note[1] == 'b' && note[0] == 'A')
n++;
else if (note[1] == '#')
n++;
else if (note[1] == 'b')
n--;
else
n = n + 0;
if (specialCase)
{
n += 9;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}
else if (note[0] == 'A')
noteHz = hz;
else if(note[0] == 'B' && specialCase == false)
{
n += 2;
temp = pow(2, n/12);
noteHz = hz * round(temp);
}
else if (note[0] == 'C')
{
n += 9;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}
else if(note[0] == 'D')
{
n += 7;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}
else if(note[0] == 'E')
{
n += 5;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}
else if(note[0] == 'F')
{
n += 4;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}
else if(note[0] == 'G')
{
n += 2;
temp = pow(2, n/12);
noteHz = hz / round(temp);
}
else
n += 0;
}
I hope code is clear I have some comments on it.
EDIT : 19.1.18
right now I have just changed n+= 0 for return 0 in last else statement but I have the same error and when I change an error :
error: expected expression else if (note[0] == 'A')
If I change all else if
for if
the same (control reaches end of non-void function) error orrcures.
Change in the
else
n = n + 0; for `return 0` didnt help (still control ...)
EDIT 20.1.18
after another advice from user Blauelf my code looks like this: and compiles :) Thank you
int frequency(string note)
{
int n = 0; // fraction(?) counter (n/12)
int hz = 440; //Hz of A in the Octave
int noteHz ;
bool specialCase = false;
float temp ;
//2nd step: calculate the A hrtz of octave
if (note[strlen(note) - 1] < 4)
{
for (int i = 1; 4 - i != strlen(note); i++)
hz /= 2;
}
else if(note[strlen(note) - 1] > 4)
{
for (int j = 1; 4 + j != strlen(note); j++)
hz *= 2;
}
else
hz = 440;
//1st step: check if we do have special representation of notes or "normal"
if (note[1] == '#' && note[0] == 'B')//- this was my 1st code in this place this below (code with use of strncmp):
{
hz *=2;
specialCase = true;
}
/*else*/ if (note[1] == 'b' && note[0] == 'A')
n++;
/*else*/ if (note[1] == '#')
n++;
/*else*/ if (note[1] == 'b')
n--;
else
n += 0;
if (specialCase)
{
n += 9;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}
else if (note[0] == 'A')
{
noteHz = hz;
return noteHz;
}
else if(note[0] == 'B' && specialCase == false)
{
n += 2;
temp = pow(2, n/12);
noteHz = hz * round(temp);
return noteHz;
}
else if (note[0] == 'C')
{
n += 9;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}
else if(note[0] == 'D')
{
n += 7;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}
else if(note[0] == 'E')
{
n += 5;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}
else if(note[0] == 'F')
{
n += 4;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}
else if(note[0] == 'G')
{
n += 2;
temp = pow(2, n/12);
noteHz = hz / round(temp);
return noteHz;
}
else
return 0;
note[strlen(note)-1]
to number 4, my attempt would be much different, but I understand why the compiler is complaining, and it definitely should)