Infinite while loop in lecture 25

Trying to end program when I answer no when asked if I want to play again. If I type no, it just starts again. Any ideas what is happening?

My Code

a guess. in your return statement you are returning == ‘y’ || ‘Y’. Since ‘Y’ is not compared to anything, perhaps you are actually returning ‘Y’, which is then cast to boolean from the ASCII value of ‘Y’(I believe its 89), and so your statement is always true?

That’s probably the line that’s causing problems, and you’re pretty close on the explanation. What happens is it’ll first evaluate 'Y' || 'y', which will convert both chars to bools, and since both are nonzero they’ll be true. Then it evaluates Response[0] == true, and since the right side is a bool it converts the left side to a bool, which is also most likely nonzero and therefore true. true == true, so the function always returns true. What you want is something like Response[0] == 'y' || Response[0] == 'Y'.

Thank you for the suggestion. I tried this, and it didn’t work. Then I restarted Xcode, tried it again. Still not working. Is there anything else there? Thanks again.

Restarted my mac, and for some reason now it works! Great spotting.

Privacy & Terms