0

> 
> 
> 
> int frequency(string note)
          {

char n = note[0]; //defines the octave of the note char o = note[strlen(note) - 1]; //checks if the note is within constraints if( n < 'A' || n > 'G') return 1; else if ( o < '0' || o > '8') return 1;

//sets fourth octave as the reference int d = o - 4; //of refers to the octave frequency int of = round ( 440 * pow(2,d));

int frequency; float x;

//x varies for different semitones switch(n) { case 'A': x = 0.;

case 'B': x= 2.;

case 'C': x = -9.;

case'D': x = -7.;

case 'E': x = -5.;

case 'F': x = -4.;

case 'G': x = -2.;

default: return 1; } //normal frequency for notes without accidentals int nf = round(ofpow(2, x/12)); //frequency to semitone before or after accidentals float af = ofpow(2, x/12));

//treats accidentals if(note[1] = '#') { frequency = round(af*pow(2.,1./12.)); return (frequency); } else if(note[1] = 'b') { frequency = round(af/pow(2.,1./12.)); return (frequency); } return (nf); }

1

Note that switch-case in C will run any code starting with the first condition that applies. This makes it possible to have code like

switch(num)
{
    case 1:
    case 2:
    case 3:
        printf("A few\n");
        break; // leave switch
    case 4:
    case 5:
        printf("Many\n");
        break; // leave switch
    default:
        printf("Dunno\n");
}

Without a break;, execution continues to the next case. In your statement, you always end up at the return 1;.

BTW: On Stack Exchange, use the {} button or press Ctrl+K after selecting code to indent it by four spaces. Text with four spaces before each line is considered code. > is meant for replies, citing parts of another message.

3
  • Thankyou for the valuable advice and as anyone can tell i am new to coding.... i did add the break blocks and yet the program keeps returning massive absurd frequency values..!!.. Feb 2 '18 at 13:43
  • Your octave seems to have an offset of 48 (because you're confusing ASCII digits and the value they represent), giving you a far too high frequency, have you fixed that?
    – Blauelf
    Feb 2 '18 at 14:42
  • That helps....The code works just fine..!! Feb 3 '18 at 17:19

You must log in to answer this question.

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