0
    bool is_rest(string s)
{
if (strcmp(s, ""))
    {
    return 0;
    }
else
    {
    return 1;
    }
}

I'm having some very strange problems with the bool is_rest function. The code I have actually works and passes check50, but when I attempt to generate a song with:

./synthesize jeopardy.wav < songs/jeopardy.txt

the song it generates is 13 hours long and silent. My best guess is that somehow my function is detecting all notes as rests, but I have no idea how. Other weird things is that I was taught that 0 == False and 1 == True in bools, but this function returns an error of runtime error: load of null pointer of type 'char' Segmentation fault

    if (strcmp(s, ""))
    {
    return 1;
}

when surely I would return 1 here if the compared strings are indeed the same?

3

Hmmm....

Re: bools being returned - While 0 always means false, the case for true varies for different languages. Some interpret only 1 as true. In c, any non-0 number (including negatives) are interpreted as true. Best practice is to always use true or false instead of numbers to avoid confusion.

In logic courses, 0 is false and 1 is true is the standard. That's probably where your learned convention arose.

As for the seg fault, which line of code is triggering it? I'd suspect an issue with the data that is being passed to the function. If the string is null rather than an empty line with a \0 string terminator, that will definitely cause issues.

If you're creating a 13 hour song, it sounds to me like an infinite or near-infinite loop is being created. Either that or something is being mishandled. I suspect the problem lies elsewhere in the code.

Lots of questions to be looked at here.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

8
  • Re: bools being returned So I've changed the code to this bool is_rest(string s) { if (strcmp(s, "")) { return false; } else { return true; } } But I'm still confused. Logic tells me that I should be returning true if the strings indeed match, but running check50 like that identifies "A4" as a rest and " " as a note yet apparently the opposite is true. – Coal143 Apr 25 '18 at 4:57
  • Fascinatingly, simply changing the numbers to true and false instead of 0 and 1 has actually solved the synthesize issue and generates the correct song...but I honestly have no idea why – Coal143 Apr 25 '18 at 5:00
  • Your instincts are correct. You should return true if the if condition is true. Perhaps you're being confused by the programming standard that says return 0 if the program executes correctly and return another number - an error code - if it fails - which is exactly the opposite logic. – Cliff B Apr 25 '18 at 5:01
  • How did I miss that??? You were returning 0 (false) when you should have been returning 1 (true). Now, you see why you should always use true or false and not numbers! – Cliff B Apr 25 '18 at 5:03
  • I think there's some confusion with strcmp, which returns not a boolean, but 0 if the strings are the same, and a value <0 or >0 if they aren't, comparing lexicographically. That's why I tend to use like if (strcmp(s, "") == 0), or in this case, if (s[0] == '\0'), and have the matching case instead of the non-matching case first. – Blauelf Apr 25 '18 at 9:00

You must log in to answer this question.

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