0

please help! I've been trying to find out whats the problem in this code for a very long time now... its not even compliling .. #pset3 cs50 #music

// Helper functions for music

#include <c s50.h>
#include < stdio.h>
#include " helpers.h"
#include < string.h>
#include < ctype.h>
#include < math.h>

// Converts a fraction formatted as X/Y to eighths
int duration(string fraction)
{

    if(fraction[2] == '2')
    {
        return 4;
    }

    else if (fraction[2]== '4')
    {
        return 2;
    }
    else if (fraction[2] == '8')
    {
        if(fraction[0] == '1')
        {
            return 1;

        }
        else if(fraction[0] == '3')
        {
            return 3;
        }
    }
    return 1;
}

// Calculates frequency (in Hz) of a note

int frequency (string note)
{
    int octave = note[strlen(note) - 1];

    octave = octave - 48;

    double f = 0.0;

    switch(note[0])
    {
        case 'C': f = 440 * pow(2.0, 9.0 / 12.0);
            break;
        case 'D': f = 440 * pow(2.0, 7.0 / 12.0);
            break;
        case 'E': f = 440 * pow(2.0, 5.0 / 12.0);
            break;
        case 'F': f = 440 * pow(2.0, 4.0 / 12.0);
            break;
        case 'G': f = 440 * pow(2.0, 2.0 / 12.0);
            break;
        case 'A': f = 440 ;
            break;
        case 'B': f = 440 * pow(2.0, 2.0 / 12.0);
            break;
        default : return 0;
    }
     //

     if(octave > 4)
     {
         for(int i = 0; i < octave - 4; ++i)
         {
             f= f*2;
         }
     }

     else
     {
         for(int i = 0; i < 4 - ocatve; ++i)
         {
             f =f/ 2;
         }
     }

    if(note[1] == 'b')
    {
        f=f / pow(2.0, 1.0/12.0);
    }
    else if(note[1] == '#')
    {
        f = f * pow(2.0, 1.0/12.0);
    }

    int ret = (int)(round (f));
    return ret;
}


// Determines whether a string represents a rest
bool is_rest(string s)
{
    if (strcmp(s, "") == 0) // if user presses enter
    {
        return true ;
    }

    else
    {
        return false ;
    }
}
0
1

For starters, get rid of all the spaces in the include statements inside the < brackets>. That'll leave one typo, a misspelled word, to be fixed so you can compile. Then take a deep breath and relax!

Next, spend some time testing your code. The frequency calculation isn't right. Take a look at the output for ./notes 4 and check each frequency against what it should be. Something is definitely off. It's also sneaking past some of the tests in check50.

If this 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 .