khuang8493
7/6/2018 - 12:37 AM

Error for conditions

Error of lots of different "if" conditions. "if" need to cover the whole rang, all the possibilities.

error: control may reach end of non-void function [-Werror,-Wreturn-type]

When you get this error, means that your "if" didn't cover all the possibilities. 

For example:

//This function is for calculating the frequency of an inputed note. It's possible that the note entered is 2 char string or 3 char string. That's why we need the "if" statements.

int frequency(string note)
{

	// check the length of the input string to see if it's 2 char or 3 char
		int l = strlen(note);

	// If only 2 char, can use what we already did in frequency1.c
	    if (l<2 || l>3)
	    {
	        return 0;
	    }

  		else if (l == 2)
  		{
          char s[2] = {note[1], '\0'};
          int octave = atoi(s);
          return (int)(27.5 * pow(2, octave));
      }

    // if there are 3 char, calculate accordingly

        else if (l == 3)
        {
          //codes for calculating accordingly
        }
        else
        {
            return 0;
        }
}

In this function, if we only write "if(l==2)" and "if(l==3)", then "if" didn't cover all the possibilities.

So we have to add 	    
      if (l<2 || l>3)
	    {
	        return 0;
	    }
Only this way can cover the whole range. Without this part, will have the error we saw at the top.