0

I have been working on Helpers.c for an extremely long time now, and I have little help with the style of the program due to my doing this totally on my own. When I run check50 from the 2018 pset3 doc, it says "timed out while waiting for program to exit".

Here is my code:

// Helper functions for music
// int values can't be added to strings that return answers to calling program
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "helpers.h"

// Converts a fraction formatted as X/Y to eighths
int duration(string fraction)
{
    //gets length
    string u = get_string("Length: ");

    //converts characters to ints
    int h = u[0] - 48;
    int g = u[2] - 48;

    //changes ints to number of 8th notes
    h *= (8/g);

    return 0;
    fraction = '0' + h;
}

// Calculates frequency (in Hz) of a t
int frequency(string note)
{
    string q = get_string("t: ");
    int i = q[0] - 64;

    if (q[1] == 'b')
    {
        i--;
        i += ((q[2] - 48) * 12);
    }
    else if (q[1] == '#')
    {
        i++;
        i += ((q[2] - 48) * 12);
    }
    else
    {
        i += ((q[1] - 48) * 12);
    }

    if (i > 103 || i < 13)
    {
        printf("Please enter an eligible note\n");
        return 1;
    }
    else
    {
        int a = 0;
        if (i > 49)
        {

            a = 440 * pow(2, (i - 49) / 12);
        }
        else if (i < 49)
        {
            a = 440 / pow(2, (49 - i) / 12);
        }
        else
        {
            a = 440;
        }
        return 0;
        note = a;
    }
}

// Determines whether a string represents a rest
bool is_rest(string s)
{
    string l = get_string();
    int z = strcmp(l, "");
    if (z == 0)
    {
        return 0;
        s = "Is rest";
    }
    else
    {
        return 0;
        s = "Is not a rest";
    }
}

Does anyone have some tips that might help? Thanks!

0

Your issue is that you are asking for user input when you shouldn't be.

/ Converts a fraction formatted as X/Y to eighths
int duration(string fraction)
{
    //gets length
    string u = get_string("Length: ");

Notice that the function is given the string called fraction. That is the string that you must use to calculate the duration. You should not be prompting the user for one.

| improve this answer | |
  • @curioskiwi - I have implemented this, but what would the number of eighths output to- I tried outputting it to duration and it wouldn't compile. What should the final result of each function output to? – D M Jul 19 '18 at 13:17
  • All you need to do is return value; where value is the variable that has the duration. In your example, you would return h; – curiouskiwi Jul 19 '18 at 20:48

You must log in to answer this question.

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