0

I'm getting a runtime error when trying to run notes. This is the first time I've worked with multiple files so I don't even know how to use the dubugger, etc..

The error I see when trying to run is:

C4: 28 helpers.c:80:12: runtime error: value inf is outside the range of representable values of type 'int' C#4: -2147483648 D4: -2147483648 D#4: -2147483648 E4: -2147483648 F4: -2147483648 F#4: -2147483648 G4: -2147483648 G#4: -2147483648 A4: -2147483648 A#4: -2147483648 B4: -2147483648

Here's my code.

// Helper functions for music

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

// Converts a fraction formatted as X/Y to eighths
int duration(string fraction)
{
    int num = atoi(&fraction[0]);
    int denom = atoi(&fraction[2]);
    int n8 = (num/denom) * 8;

    return n8;
}

// Calculates frequency (in Hz) of a note
int frequency(string note)
{
    char note1 = note[0];
    int num;
    int oct;
    float freq;

//gives each note a value
    if (strcmp(&note1,"C") == 0)
    {
        num = 3;
    }
    if (strcmp(&note1,"D") == 0)
    {
        num = 5;
    }
    if (strcmp(&note1,"E") == 0)
    {
        num = 7;
    }
    if (strcmp(&note1,"F") == 0)
    {
        num = 8;
    }
    if (strcmp(&note1,"G") == 0)
    {
        num = 10;
    }
    if (strcmp(&note1,"A") == 0)
    {
        num = 0;
    }
    if (strcmp(&note1,"B") == 0)
    {
        num = 2;
    }

//checks for accidental and adjusts semitone
    if (strlen(note) == 3 && strcmp(&note[1],"b") == 0)
    {
        num--;
    }
    if (strlen(note) == 3 && strcmp(&note[1],"#") == 0)
    {
        num++;
    }


//checks for accidental then sets octave number
    if (strlen(note) == 2)
    {
        oct = atoi(&note[2]);
    }
    else
    {
        oct = atoi(&note[1]);
    }


    freq = 440 * pow(2,(num / 12));

    return round(freq * pow(2,(oct - 4)));

}

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

I've checked other posts with a similar error, but I don't understand how this error relates to my code. Please help me!

0

You aren't initializing num so it contains garbage values.

Why are you using strcmp and &note1 instead of simply comparing the chars like

if (note1 = 'C')

Be sure to double-check the value you are giving to num. If A4 is the zero-base case, wouldn't C be below A and therefore contribute a negative value? Most of your settings aren't correct there.

Also, when you calculate the octave, if the strlen of note is 2, then note[2] is not in the array, so calling atoi(&note[2]) is going to give you some unknown value.

atoi isn't an appropriate function to use when you are dealing with chars. Why not simply use the ascii value and subtract as you did in caesar/vigenere. Just like 'c' - 'a' is 2, '4' - '0' is 4.

1
  • Lots of little things that needed changing. Thanks for the nod in the right direction. I was using double quotes so the compiler kept advising I use strcmp. Oct 4 '18 at 20:00

You must log in to answer this question.

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