Timeline for Pset3 Music - Issues with bool is_rest function
Current License: CC BY-SA 3.0
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 26, 2018 at 6:47 | comment | added | user21490 | You're both kinda right. is_rest WAS returning the opposite of what it was supposed to return, but I only wrote it that way because writing it the "proper" way actually just caused a segmentation fault. Returning True and False instead of 0 and 1 has killed both of these birds with one stone as it were | |
Apr 26, 2018 at 6:45 | comment | added | Blauelf |
I completely agree, but still assume this mistake was based on a misperception around the return value of strcmp .
|
|
Apr 25, 2018 at 18:49 | comment | added | Cliff B | No, @blauelf, the problem was with the return values of is_rest. It was returning the opposite of what it was supposed to return. | |
Apr 25, 2018 at 9:00 | comment | added | Blauelf |
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.
|
|
Apr 25, 2018 at 5:07 | vote | accept | CommunityBot | ||
Apr 25, 2018 at 5:03 | comment | added | Cliff B | 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! | |
Apr 25, 2018 at 5:01 | comment | added | Cliff B | 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. | |
Apr 25, 2018 at 5:00 | comment | added | user21490 | 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 | |
Apr 25, 2018 at 4:57 | comment | added | user21490 |
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.
|
|
Apr 25, 2018 at 4:18 | history | answered | Cliff B | CC BY-SA 3.0 |