0

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;
2
  • 2nd step before 1st step. Doing it right :D (don't get any of the two parts, especially how you compare character 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)
    – Blauelf
    Commented Jan 17, 2018 at 16:37
  • And starting value 0 doesn't care whether you multiply or divide it by 2.
    – Blauelf
    Commented Jan 17, 2018 at 16:39

1 Answer 1

2

This error message means "Your function is declared as returning a value of type int, but it's possible it might return not at all".

Make sure control flow always ends in a return statement.

Have you tried help50? It might have told you about the same when called like help50 make music.

6
  • I have tryied but still nothing more. It is good that you saw this hz problem and I have changed it but now i still got it even though i think I have covered all of the possibilities (If some of chars appears we do something if not we add + to n )
    – koko loko
    Commented Jan 18, 2018 at 4:51
  • Have you added some return statement, as the error message demanded?
    – Blauelf
    Commented Jan 18, 2018 at 12:10
  • yes I added return to the last else statement. Right now I am getting the error "expected expression" for: else if (note[0] == 'A') noteHz = hz; I added the { } for the declaration part but the same error is occurring
    – koko loko
    Commented Jan 19, 2018 at 7:00
  • The code you posted does compile after adding a return statement at the end (it just doesn't do what you want it to do). How am I going to guess what else you have changed? And is that still the original question?
    – Blauelf
    Commented Jan 19, 2018 at 14:18
  • sorry ;) i forgot how does this site works.
    – koko loko
    Commented Jan 19, 2018 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .