0

I was wondering what is wrong with these if else statements because this program is not compiling. Any information would be appreciated and any advice please.

#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

int hertz(string note);

int main(void)
{
string note = get_string("Piano key: ");
hertz(note);
return 0;
}

int hertz(string note)
{
int noteLength = 0, octave, answer;
char accidental;

for(int i = 0, n = strlen(note); i < n; i++)
    noteLength++;

if(noteLength != 2 || noteLength != 3)
    return 0;

if(noteLength == 2)
{
     octave = atoi(&note[1]);
     if(octave == 4)
     {
        if(note[0] == 'A')
            return 440;
        else if(note[0] == 'B')
            return 494;
        else if(note[0] == 'C')
            return 261;
        else if(note[0] == 'D')
            return 294;
        else if(note[0] == 'E')
            return 330;
        else if(note[0] == 'F')
            return 349;
        else if(note[0] == 'G')
            return 392;
        else
            return 0;
     }

     else if(octave > 4)
     {
         int difference = 0, shade = 0;

         for(int i = 4; i < octave; i++)
         difference++;

        if(note[0] == 'G')
            answer = (440 / pow((double)2, (double)2/12));
        else if(note[0] == 'F')
            answer = (440 / pow((double)2, (double)4/12));
        else if(note[0] == 'E')
            answer = (440 / pow((double)2, (double)5/12));
        else if(note[0] == 'D')
            answer = (440 / pow((double)2, (double)7/12));
        else if(note[0] == 'C')
            answer = (440 / pow((double)2, (double)9/12));
        else if(note[0] == 'A')
            answer = 440;
        else if(note[0] == 'B')
            answer = (440 * pow((double)2, (double)2/12));
        else
            return 0;

         while(shade != difference)
         {
            answer *= 2;
            shade++;
         }
         return answer;
     }

     else if(octave < 4)
    {
       int difference = 0, shade = 0;

         for(int i = 4; i < octave; i++)
         difference++;

        if(note[0] == 'G')
            answer = (440 / pow((double)2, (double)2/12));
        else if(note[0] == 'F')
            answer = (440 / pow((double)2, (double)4/12));
        else if(note[0] == 'E')
            answer = (440 / pow((double)2, (double)5/12));
        else if(note[0] == 'D')
            answer = (440 / pow((double)2, (double)7/12));
        else if(note[0] == 'C')
            answer = (440 / pow((double)2, (double)9/12));
        else if(note[0] == 'A')
            answer = 440;
        else if(note[0] == 'B')
            answer = (440 * pow((double)2, (double)2/12));
        else
            return 0;

         while(shade != difference)
         {
            answer /= 2;
            shade++;
         }
         return answer;
    }

}

else if(noteLength == 3)
{
    octave = atoi(&note[2]);
    accidental = note[1];

    if(accidental != '#' || accidental != 'b')
        return 0;

    if(octave == 4)
     {
        if(note[0] == 'A' && accidental == '#')
            answer = (440 * pow((double)2, (double)1/12));
            return answer;
        else if(note[0] == 'A' && accidental == 'b')
            answer = (440 / pow((double)2, (double)1/12));
            return answer;
        else if(note[0] == 'G' && accidental == '#')
            answer = (440 / pow((double)2, (double)1/12));
            return answer;
        else if(note[0] == 'G' && accidental == 'b')
            answer = (440 / pow((double)2, (double)3/12));
            return answer;
        else if(note[0] == 'F' && accidental == '#')
            answer = (440 / pow((double)2, (double)3/12));
            return answer;
        else if(note[0] == 'F' && accidental == 'b')
            answer = (440 / pow((double)2, (double)5/12));
            return answer;
        else if(note[0] == 'E' && accidental == '#')
            answer = (440 / pow((double)2, (double)4/12));
            return answer;
        else if(note[0] == 'E' && accidental == 'b')
            answer = (440 / pow((double)2, (double)6/12));
            return answer;
        else if(note[0] == 'D' && accidental == '#')
            answer = (440 / pow((double)2, (double)6/12));
            return answer;
        else if(note[0] == 'D' && accidental == 'b')
            answer = (440 / pow((double)2, (double)8/12));
            return answer;
        else if(note[0] == 'C' && accidental == '#')
            answer = (440 / pow((double)2, (double)8/12));
            return answer;
        else if(note[0] == 'C' && accidental == 'b')
            answer = (440 / pow((double)2, (double)10/12));
            return answer;
        else if(note[0] == 'B' && accidental == '#')
            answer = (440 * pow((double)2, (double)3/12));
            return answer;
        else if(note[0] == 'B' && accidental == 'b')
            answer = (440 * pow((double)2, (double)1/12));
            return answer;
        else
            return 0;
     }

     else if(octave > 4)
     {
         int difference = 0, shade = 0;

         if(note[0] == 'A' && accidental == '#')
            answer = (440 * pow((double)2, (double)1/12));
        else if(note[0] == 'A' && accidental == 'b')
            answer = (440 / pow((double)2, (double)1/12));
        else if(note[0] == 'G' && accidental == '#')
            answer = (440 / pow((double)2, (double)1/12));
        else if(note[0] == 'G' && accidental == 'b')
            answer = (440 / pow((double)2, (double)3/12));
        else if(note[0] == 'F' && accidental == '#')
            answer = (440 / pow((double)2, (double)3/12));
        else if(note[0] == 'F' && accidental == 'b')
            answer = (440 / pow((double)2, (double)5/12));
        else if(note[0] == 'E' && accidental == '#')
            answer = (440 / pow((double)2, (double)4/12));
        else if(note[0] == 'E' && accidental == 'b')
            answer = (440 / pow((double)2, (double)6/12));
        else if(note[0] == 'D' && accidental == '#')
            answer = (440 / pow((double)2, (double)6/12));
        else if(note[0] == 'D' && accidental == 'b')
            answer = (440 / pow((double)2, (double)8/12));
        else if(note[0] == 'C' && accidental == '#')
            answer = (440 / pow((double)2, (double)8/12));
        else if(note[0] == 'C' && accidental == 'b')
            answer = (440 / pow((double)2, (double)10/12));
        else if(note[0] == 'B' && accidental == '#')
            answer = (440 * pow((double)2, (double)3/12));
        else if(note[0] == 'B' && accidental == 'b')
            answer = (440 * pow((double)2, (double)1/12));
        else
            return 0;

         for(int i = 4; i < octave; i++)
         difference++;

         while(shade != difference)
         {
            answer *= 2;
            shade++;
         }
         return answer;
     }

     else if(octave < 4)
    {
       int difference = 0, shade = 0;

       if(note[0] == 'A' && accidental == '#')
            answer = (440 * pow((double)2, (double)1/12));
        else if(note[0] == 'A' && accidental == 'b')
            answer = (440 / pow((double)2, (double)1/12));
        else if(note[0] == 'G' && accidental == '#')
            answer = (440 / pow((double)2, (double)1/12));;
        else if(note[0] == 'G' && accidental == 'b')
            answer = (440 / pow((double)2, (double)3/12));
        else if(note[0] == 'F' && accidental == '#')
            answer = (440 / pow((double)2, (double)3/12));
        else if(note[0] == 'F' && accidental == 'b')
            answer = (440 / pow((double)2, (double)5/12));
        else if(note[0] == 'E' && accidental == '#')
            answer = (440 / pow((double)2, (double)4/12));
        else if(note[0] == 'E' && accidental == 'b')
            answer = (440 / pow((double)2, (double)6/12));
        else if(note[0] == 'D' && accidental == '#')
            answer = (440 / pow((double)2, (double)6/12));
        else if(note[0] == 'D' && accidental == 'b')
            answer = (440 / pow((double)2, (double)8/12));
        else if(note[0] == 'C' && accidental == '#')
            answer = (440 / pow((double)2, (double)8/12));
        else if(note[0] == 'C' && accidental == 'b')
            answer = (440 / pow((double)2, (double)10/12));
        else if(note[0] == 'B' && accidental == '#')
            answer = (440 * pow((double)2, (double)3/12));
        else if(note[0] == 'B' && accidental == 'b')
            answer = (440 * pow((double)2, (double)1/12));
        else
            return 0;

         for(int i = 4; i > octave; i--)
         difference++;

         while(shade != difference)
         {
            answer /= 2;
            shade++;
         }
         return answer;
    }
}

}

1 Answer 1

1

Look at the following code:

if(octave == 4)
 {
    if(note[0] == 'A' && accidental == '#')
        answer = (440 * pow((double)2, (double)1/12));
        return answer;
    else if(note[0] == 'A' && accidental == 'b')

Since there's no curly brace pair {} associated with the if statement, only the first line following the closing parenthesis is associated with the if statement. That means the return answer; line WILL execute! It also breaks the association between the first if statement and the else if statement that follows. This error repeats in the code that follows.

Perhaps a review of the class material on if/else if/else and curly braces to create code blocks is in order? ;-)

If that answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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