0

// Helper functions for music

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

// Converts a fraction formatted as X/Y to eighths
int duration(string fraction)
{
    int numerator = fraction[0] - 48;
    int denominator = fraction[2] - 48;
    int num8 = (numerator/denominator)/(0.125);
    return num8;
}

// Determines whether a string represents a rest
bool is_rest(string s)
{
    if (strcmp(s, " "))
    {
        return true;
    }
    else
    {
        return false;
    }
}

I'm running into problems with is_rest() and duration(). According to the result from checkcs50, all the durations are 0 regardless of the input values and is_rest cannot identify "A4" as not a rest.

Can someone tell me what's wrong with the code? I have been stuck on this for a while.

Thanks!

2

The duration problem is based in the integer division of the formula.

int num8 = (numerator/denominator)/(0.125);

It's doing integer division of numerator/denominator. In regular division, this will always be less than 1, but since this is integer division, it always evaluates to 0. Remember, in integer division, the result is always truncated. You should think about different ways to sequence the operations to get to the answer. ;-)

The rest problem is even simpler.

if (strcmp(s, " "))

What do the instructions define as representing a rest? Is that different than a space?

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 .